Tanner Ruski

Tanner Ruski

  • NA
  • 4
  • 480

How to make a Save-file-Dialog box after downloaded file.

Feb 13 2019 8:22 PM
Description:
So I made this following script to download a file from the internet with a progress bar showing what percentage the download is at and custom message boxs. Now I have the files being saved to the users %TEMP% path. And it uses events to prevent people from clicking on the button again and starting a new download.

Problem:

I want to give the user a choice of where to save the file, but show his temp path as the default location. (Like a Save-file-dialog Box)
I'm still fairly new to coding and don't know where to exactly start.

What I tried:
I didn't try any code, but I did go around google and try to find a solution.
Here are some websites that I found that might be useful.

https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-save-files-using-the-savefiledialog-component
https://www.c-sharpcorner.com/UploadFile/mahesh/savefiledialog-in-C-Sharp/


They explain it really well. But I don't know how to incorporate it into this script.
And I dont want to write a brand new script. Any Help would be Appreciated!

My Code:
private bool _isBusy = false; 
private void button1_Click(object sender, EventArgs e)     => DownloadFile("someurl1", "somefilename1.exe"); 
private void button2_Click(object sender, EventArgs e)    => DownloadFile("someurl2", "somefilename2.exe"); 
private void button3_Click(object sender, EventArgs e)    => DownloadFile("someurl3", "somefilename3.exe"); 
private void button4_Click(object sender, EventArgs e)    => DownloadFile("someurl4", "somefilename4.exe"); 
private void DownloadFile(string url, string fileName) { 

   if(_isBusy) return; 
   _isBusy = true; 
   var output = Path.Combine(Path.GetTempPath(), fileName);    MessageBox.Show($"{fileName} will start downloading from {url}"); 
   using (WebClient client = new WebClient())    { 
      client.DownloadFileCompleted += (sender, args) => 
                                          {                                              MessageBox.Show($"{fileName} Complete!");                                              Process.Start(output);                                                                                            _isBusy = false;                                           }; 
      client.DownloadProgressChanged += (sender, args) => progressBar1.Value = args.ProgressPercentage;              client.DownloadFileAsync(new Uri(url), output);       }    } }

Answers (1)