Home > Enterprise >  C# Downloading a file with (WebClient) to a specific path wont work
C# Downloading a file with (WebClient) to a specific path wont work

Time:09-11

I'm learning C# right now and I'm making a kind of tool where users can download certain files. However, I would like the downloaded file to end up in the Downloads folder and not in "bin\Realse"

Path was also created before: [(System.IO.Directory.CreateDirectory("C:\Users\" Environment.UserName.ToString() "\Downloads\Glebi-Tool\Games");) ]

Code:

private void btnMinecraft_Click(object sender, EventArgs e)
    {
        wc.DownloadFileCompleted  = new AsyncCompletedEventHandler(FileDownloadComplete1);
        Uri rarurl = new Uri("https://cdn.discordapp.com/attachments/1016411808887746570/1016422145229848686/MfW10_Fix_Repair_UWP_Generic.rar");
        wc.DownloadFileAsync(rarurl, "MfW10_Fix_Repair_UWP_Generic.rar", @"C:\\Users\\"   Environment.UserName()   "\\Downloads\\Glebi-Tool\\Games");
    }

CodePudding user response:

So I manged to figure it out myself. Here is the Answer:

    private void btnMinecraft_Click(object sender, EventArgs e)
    {

        if (System.IO.File.Exists(@"C:\\Users\\"   Environment.UserName   "\\Downloads\\Glebi-Tool\\Games\\MfW10_Fix_Repair_UWP_Generic.rar"))
        {
            MessageBox.Show("Already Downlaoded");
        }
        else
        {
            {
                using (var wc = new WebClient())

                    wc.DownloadFile("https://cdn.discordapp.com/attachments/1016411808887746570/1016422145229848686/MfW10_Fix_Repair_UWP_Generic.rar", "MfW10_Fix_Repair_UWP_Generic.rar");
            }

            string fromPath = Path.Combine(Application.StartupPath, "MfW10_Fix_Repair_UWP_Generic.rar");
            string toPath = Path.Combine(@"C:\\Users\\"   Environment.UserName   "\\Downloads\\Glebi-Tool\\Games", "MfW10_Fix_Repair_UWP_Generic.rar");

            // Move the file.
            File.Move(fromPath, toPath);

            MessageBox.Show("Download Completed");
        }
    }
  • Related