Search code examples
c#.netformsappdata

Download and Move a file into a folder located in Local App Data C#


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.


Solution

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