WebClient DownloadFile throws UnauthorizedAccessException



我想下载一些文件并在%temp%文件夹中执行它们,但是当我尝试运行它时System.UnauthorizedAccessException

Exceção sem Tratamento: System.Net.WebException: Exceção durante uma solicitação do WebClient.---> System.UnauthorizedAccessException: O acesso ao caminho 'C:\Users\Muni\AppData\Local\Temp' foi negado. em System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
em System.IO.FileStream.Init(String path, FileMode mode, FileAccess 访问, Int32 权限, 布尔使用权限, 文件共享共享, Int32 缓冲区大小, 文件选项选项, SECURITY_ATTRIBUTES 秒收件人, 字符串 msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
em System.IO.FileStream..ctor(字符串路径、文件模式模式、文件访问) access) em System.Net.WebClient.DownloadFile(Uri address, String fileName) --- Fim do rastreamento de pilha de exceções internas --- em System.Net.WebClient.DownloadFile(Uri address, String fileName)
em System.Net.WebClient.DownloadFile(String address, String fileName) EM 加密。Program.Main(String[] args) na C:\Users\Muni\source\repos\encrypt\encrypt\program.cs

try
{
string name = Environment.UserName;
WebClient Client = new WebClient();
string temp = "C:\Users\" + name + "\AppData\Local\Temp";
Client.DownloadFile("LINK", temp);
}
catch
{
return;
}

您没有在路径中提供文件名。

这是我为测试而编写的错误示例:

string name = Environment.UserName;
WebClient Client = new WebClient();
string path = "C:\";
Client.DownloadFile("https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", path);

当我运行它时,我得到同样的异常。

当我在路径中输入文件名时,它可以正常工作。

string name = Environment.UserName;
WebClient Client = new WebClient();
string path = "C:\test.png";
Client.DownloadFile("https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", path);

这个问题有一个答案,指出UnauthorizedAccessException异常的原因之一是"路径是一个目录"。

相关内容

  • 没有找到相关文章

最新更新