1

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");
    }
glebi
  • 11
  • 3
  • Take a look at [this](https://stackoverflow.com/questions/10667012/getting-downloads-folder-in-c). – Andy Sep 08 '22 at 21:50
  • The [Verbatim identifier](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim) is not used like that -- What does *won't work* mean here? Do you get any exception? Or simply the file is not created? Have you tried to add an event handler to the `DownloadProgressChanged` event, to see whether the download actually starts? -- You're adding an event handler to the same WebClient object each time you click the Button -- Use `Path.Combine()` to build paths. Or generate a new `Uri` from a string. – Jimi Sep 08 '22 at 21:59
  • The Verbatim identifier is not used like that -- What does won't work mean here? Do you get any exceptions? – Hediye_seza Sep 10 '22 at 06:48

1 Answers1

0

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");
        }
    }
glebi
  • 11
  • 3