代码之家  ›  专栏  ›  技术社区  ›  DSADSADSADSA142

WebClient DownloadFile引发UnauthorizedAccessException[关闭]

  •  -5
  • DSADSADSADSA142  · 技术社区  · 8 年前

    我想下载一些文件并在 %temp% 文件夹,但我得到 System.UnauthorizedAccessException 当我尝试运行它时:

    例外情况:系统。网Web例外:例外§o durante uma 请求§o做网络客户---&燃气轮机;系统UnauthorizedAccessException:O acesso ao Camino’C:\Users\Muni\AppData\Local\Temp’foi negado。em公司 系统IO__错误WinIOError(Int32 errorCode,字符串maybeFullPath)
    em系统。IO。文件流。Init(字符串路径、FileMode模式、FileAccess access、Int32权限、布尔用户权限、文件共享、Int32 bufferSize、FileOptions选项、SECURITY\u属性secAttrs、字符串 msgPath、布尔bFromProxy、布尔useLongPath、布尔checkHost)
    em系统。IO。文件流。。ctor(字符串路径、FileMode模式、FileAccess 访问)em系统。网网络客户端。下载文件(Uri地址,字符串 文件名)---Fim do rastreamento de pilha de exce§¦µes internas--- em系统。网网络客户端。下载文件(Uri地址、字符串文件名)
    em系统。网网络客户端。下载文件(字符串地址、字符串文件名) em加密。程序主(字符串[]args)na C: \用户\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;
            }
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Matt Hogan-Jones    8 年前

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

    下面是我为测试编写的一个错误示例:

        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);
    

    这 question 有一个答案表明 UnauthorizedAccessException 例外情况是“路径是目录”。