C#对路径的访问被拒绝。。。Client.Download(文件链接,路径)
我的代码有问题: 当我尝试从dropbox下载文件并将其放入漫游文件夹时,出现了一些问题,下面是错误日志:C#对路径的访问被拒绝。。。Client.Download(文件链接,路径),c#,asp.net,download,webclient,directory-permissions,C#,Asp.net,Download,Webclient,Directory Permissions,我的代码有问题: 当我尝试从dropbox下载文件并将其放入漫游文件夹时,出现了一些问题,下面是错误日志: System.Net.WebException: An exception occurred during a WebClient request. ---> System.UnauthorizedAccessException: Access to the path'C:\Users\Marco\AppData\Roaming\.maycraft' is
System.Net.WebException: An exception occurred during a WebClient request. ---> System.UnauthorizedAccessException: Access to the path'C:\Users\Marco\AppData\Roaming\.maycraft' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at System.Net.WebClient.DownloadFile(Uri address, String fileName)
--- End of inner exception stack trace ---
at System.Net.WebClient.DownloadFile(Uri address, String fileName)
at System.Net.WebClient.DownloadFile(String address, String fileName)
at MayCraftLauncher.Program.downloadExist() in c:\Users\Marco\Desktop\MayCraftLauncher\C# SourceCode\MayCraftLauncher\MayCraftLauncher\Program.cs:line 71
很明显,我试图用管理员权限运行Visual Studio,但什么都没有
下面是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Security.Principal;
using System.IO;
using System.IO.Compression;
using Ionic.Zip;
namespace MayCraftLauncher
{
class Program
{
public static System.Threading.Thread existDownloading = new System.Threading.Thread(downloadExist);
public static string userName = Environment.UserName;
public static string path = @"C:\Users\" + userName + @"\AppData\Roaming\.maycraft\Exist";
public static string maypath = @"C:\Users\" + userName + @"\AppData\Roaming\.maycraft";
static void Main(string[] args)
{
folderExist();
}
static void check1()
{
while (!Directory.Exists(maypath))
{
System.Threading.Thread.Sleep(5000);
}
existDownloading.Start();
}
static void folderExist()
{
if (!Directory.Exists(maypath))
{
System.Diagnostics.Process.Start(@"C:\Users\Marco\Desktop\MayCraftLauncher.jar");
check1();
}
else
{
fileExist();
}
}
static void fileExist()
{
if (File.Exists(path)) //Determina se è la prima volta che l'applicazione viene eseguita
{
Console.WriteLine("Exist");
Console.ReadLine();
}
else
{
existDownloading.Start();
Console.WriteLine("Downloading...");
}
}
static void downloadExist()
{
WebClient Client = new WebClient();
DateTime btime = DateTime.Now;
path = @"C:\Users\" + userName + @"\AppData\Roaming\.maycraft\";
string publicLinkExist = @"https://dl.dropbox.com/u/35356155/minecraft/download/Exist";
//string publicLinkMods = @"https://dl.dropbox.com/u/35356155/minecraft/download/mods.7z";
string publicLinkMods = @"https://dl.dropbox.com/u/35356155/minecraft/download/mcp.7z";
try
{
//----------------File downloading-------------------
Client.DownloadFile(publicLinkExist, path);
DirectoryInfo dev = Directory.CreateDirectory(path);
string devpath = @"C:\Users\" + userName + @"\AppData\Roaming\.maycraft";
Client.DownloadFile(publicLinkMods, devpath);
DateTime atime = DateTime.Now;
Console.WriteLine("Downloading time: " + atime.Subtract(btime).TotalSeconds + " sec.");
//---------------Files Extracion-------------------
using (ZipFile zip = ZipFile.Read(devpath))
{
zip.ExtractAll(path);
}
}
catch (Exception e)
{
Console.WriteLine(e);
System.IO.File.WriteAllText(@"C:\Users\Marco\Desktop\log.txt", e.ToString());
Console.ReadKey();
}
existDownloading.Abort();
}
}
}
它正在输出到
\Roaming.maycraft
,它不在Roaming
文件夹中,而是在AppData
根目录中,\Roaming.maycraft
将是一个文件。底线是,这与您没有写入权限的任何地方相同。正如Grant所说,您应该创建一个文件夹,在该文件夹中您可以授予ASP_NET进程权限,并从那里的DropBox下载文件。或者,如果您不需要下载和保存文件,只需要动态地使用内容,则可以使用Path.GetTempFileName获取随机生成的文件的路径,并使用StreamWriter将dropbox文件的内容写入该文件。