-2
  System.Net.WebException occurred
      HResult=0x80131509
      Message=Ett undantag uppstod under en WebClient-begäran.
      Source=System
      StackTrace:
       at System.Net.WebClient.DownloadFile(Uri address, String fileName)
       at System.Net.WebClient.DownloadFile(String address, String fileName)
       at ConsoleApp1.Program.Main(String[] args) in C:\Use`enter code here`rs\user\Documents\Visual Studio `enter code here`2017\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 20

    Inner Exception 1:
    UnauthorizedAccessException: Åtkomst till sökvägen C:\Program Files (x86)\Leakoni\overwatch.txt nekas.

So i actually doing this: but where is the problem?

using (var client = new WebClient())
        {
            client.DownloadFile("https://leakoni.net/files/overwatch.txt", path + "overwatch.txt");
        }

I don't really know what to do anymore tbh, where is my failure and how I'm supposed to fix it ;/

  • It looks to me that your process does not have the appropriate access to write the downloaded file to the location you specify - `C:\Program Files (x86)\Leakoni\overwatch.txt`. – Martin Sep 11 '18 at 16:30
  • Welcome to SO. Have you checked other answers e.g. this one: https://stackoverflow.com/questions/7715397/why-do-i-get-an-unauthorizedaccessexception-when-downloading-data – Caius Jard Sep 11 '18 at 16:31
  • tip: use `Path.Combine` to join directory and filenames together, etc.. not string concatenation. Path.Combine is more robust – Caius Jard Sep 11 '18 at 16:32
  • string path = @"C:\Program Files (x86)\Leakoni\"; is my path string. – Dovydas Bumblauskas Sep 11 '18 at 16:33
  • It's telling you (and note I do not read Swedish): whatever user context you're using to write the file *doesn't have access* to that location. Fix your permissions, or write to a more appropriate location like the user's `AppData` folder. EDIT: internet translation of error: "Accessing the c:\.... path is denied". – Marc L. Sep 11 '18 at 16:39
  • You cannot write to the C:\Program Files (x86)\Leakoni directory. Pick another one, like the Temp directory, Documents, an AppData directory, etc. – Hans Passant Sep 11 '18 at 16:42

2 Answers2

1

As per the comment from Martin Parkin, it looks like you're attempting to download a file into your program files folder. On modern windows operating systems this requires administrator permission. Even if you're logged on as an administrator you still have to approve the action in the GUI (try it now; using your normal windows GUI and a file you already have, copy it [the file] from e.g. your desktop and paste it into your Program Files:

enter image description here

Your code won't trigger the elevation process, it'll just get an access denied. Download to somewhere else instead (C:\temp) first, to satisfy yourself that it works before you set about implementing the necessary stuff to grant your program runtime access to writing data into Program Files subfolders (or change your program so it stores its data elsewhere - c:\users\username\appdata\roaming\yourcompany\ perhaps? )

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
1

I faced same issue before when trying to write xml file and fixed it by opening visual studio as a administrator right click then run as administrator.

Di Kamal
  • 173
  • 13
  • This is not a solution! Working around Windows security by running the process as Administrator is not the correct way to solve this. – Martin Sep 11 '18 at 16:47
  • 1
    In normal cases if you are trying to read/write file from specific folder you must have access to it! The simplest way if you are working on your localhost is by open it as admin. Otherwise you should have proper permission to do your work. – Di Kamal Sep 11 '18 at 16:50
  • _Otherwise you should have proper permission to do your work._ - Correct. You should grant the appropriate permissions to the account that is executing the process, not run it as Administrator – Martin Sep 11 '18 at 16:53