-1

I want to download a .ini file for Fortnite and have it move from downloads to LocalAppData however I click the button and my program closes with a error 'An exception occurred during a WebClient request.'enter image description here I left an image of my code and can anyone help me out? I need to to either download to the FortniteGame\Saved\Config\WindowsClient or I would like to be able to move the file to there. Thank You to anyone that can help.

Damian
  • 13
  • 1
  • 5
  • Welcome to Stack Overflow, please [copy paste your code instead of using a screenshot](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question), easier to help you with text. – Pac0 Oct 21 '21 at 05:11
  • 1
    Also, you may have a more specific exception text. If you have Visual Studio, you can try to open the exception object and check some properties, especially the error message (you may have other lines underneath) and the InnerException – Pac0 Oct 21 '21 at 05:13
  • and your string file looks invalid. A filename in windows can't have a `:` in it. It seems you wanted to write a path inside, but the backslashes `\\` were removed. – Pac0 Oct 21 '21 at 05:14
  • Does this answer your question? [How do I get %LocalAppData% in c#?](https://stackoverflow.com/questions/1980105/how-do-i-get-localappdata-in-c) – Pac0 Oct 21 '21 at 05:17
  • I think realistically the only error you have made is, you should have put the `string file` declaration first, and used double backslashes in the value like you did in Download... then just use `file` in the call to download, not a literal string – Caius Jard Oct 21 '21 at 05:43
  • Also, make sure the folder you're downloading to exists first. You can do it with `Directory.CreateDirectory(Path.GetDirectoryName(file))` – Caius Jard Oct 21 '21 at 05:44
  • If you post your code as text, so it's copyable, I'll fix it but I can't be bothered typing it out on a cellphone, swapping back and forth between two tabs continuously. Quite why people post code as images I've no idea; it takes more effort than just selecting the code, Ctrl c and Ctrl v into the browser. As long as there are at least 4 spaces at the start of the line it formats as monospace – Caius Jard Oct 21 '21 at 05:47
  • I just converted the image to text online :D – Manish Oct 21 '21 at 05:59

1 Answers1

1

There are a few problems with your question

  1. You have pasted the image, never post an image of the code
  2. you should always paste the inner exception while asking a question
    Inner Exception -> DirectoryNotFoundException: Could not find a part of the path 'C:\Downloads\GameUserSettings.ini'.
  3. DownloadFile method takes a file name, but you are passing path
    Reference -> https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfile?view=net-5.0

I have updated the code and it is working for me.

using (WebClient webclient = new WebClient())
               webclient.DownloadFile("https://cdn.discordapp.com/attachments/897280259219656744/897643067551645757/GameUserSettings.ini", "GameUserSettings.ini");
            string folder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            string specificFolder = Path.Combine(folder, "FortniteGame\\Saved\\Config\\WindowsClient");
            string file = @"GameUserSettings.ini";
            if (!Directory.Exists(specificFolder))
                Directory.CreateDirectory(specificFolder);
            File.Copy(file, Path.Combine(specificFolder, Path.GetFileName(file)));
Manish
  • 1,139
  • 7
  • 21