-5

I want to download some files and execute them in the %temp% folder but I get System.UnauthorizedAccessException when I try to run it:

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 access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
em System.IO.FileStream..ctor(String path, FileMode mode, FileAccess 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 encrypt.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;
        }
Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35
  • 5
    You are missing a "+" after name – Selman Genç Apr 06 '18 at 16:10
  • okay i got the temp part working but when i compile it i get System.UnauthorizedAccessException – DSADSADSADSA142 Apr 06 '18 at 16:11
  • 1
    Have you actually looked at the `Message` property on the exception? Please edit your question to correct your code and to include the message from the exception. – Matt Hogan-Jones Apr 06 '18 at 16:13
  • @DaisyShipton btw thats the whole code from the program its console based and when the person opens it downloads some stuff into the %temp% folder and executes it – DSADSADSADSA142 Apr 06 '18 at 16:14
  • 1
    @DSADSADSADSA142 please use `try/catch` and [edit] your question with the full exception. like: `try { /* Put your code here*/ } catch (Exception ex) { string strError = ex.ToString(); }`. Add the content of "strError" in this question. – Mauricio Arias Olave Apr 06 '18 at 16:14
  • 1
    [File.Copy](https://msdn.microsoft.com/en-us/library/c6cfw35a(v=vs.110).aspx) for one. – Ron Beyer Apr 06 '18 at 16:21
  • 2
    @DSADSADSADSA142 I don't want to be rude or something, but, since you speak Portuguese, you should ask here: [Stack Overflow em Português](https://pt.stackoverflow.com) – Mauricio Arias Olave Apr 06 '18 at 16:23
  • 1
    Ah, you don't have a *file name* in your code. You need to include the name of the file in the path. – Matt Hogan-Jones Apr 06 '18 at 16:26

1 Answers1

3

You are not supplying the file name in the path.

Here is an example of your mistake that I've written for testing:

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

When I run it I get the same exception.

When I put a file name in the path it works correctly.

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

This question has an answer that states that one of the causes of the UnauthorizedAccessException exception is "Path is a directory".

Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35