如何更改此脚本以支持多个文件?

时间:2019-02-02 23:47:58

标签: c#

所以我找人成功帮助我编写了一个脚本,该脚本使我可以使用进度栏从Internet下载文件。现在,我希望能有独立的多个单独的按钮,所以当我点击一个特定的按钮它会下载指定的文件。我还是新来的编码,所以我真的不知道下一步该怎么做。

string path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "SOMEFILENAME.exe");
WebClient client;

private void button4_Click(object sender, EventArgs e)
{
    if (client != null && client.IsBusy) 
            return;

    if (client == null) 
    {
        client = new WebClient(); // Create a new client here
        client.DownloadFileCompleted += client_DownloadFileCompleted;
        client.DownloadProgressChanged += client_DownloadProgressChanged; 
    }
    MessageBox.Show("File will start downloading");

    client.DownloadFileAsync(new Uri("GOOGLE DRIVE LINK"), path);
}

private void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) 
{
    MessageBox.Show("File has been downloaded!");

    System.Diagnostics.Process.Start(path);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (client != null)
        client.Dispose(); 
}

private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) // NEW
{
    progressBar1.Value = e.ProgressPercentage;
}







--------------------------------------------------------------------------
"You could do something like this, make sure you button event handlers are hooked up though"
When I click on the button I get this Error
-----------------
The Error Happens Here:       
 private void Button1_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();

What the Error Says
System.NotImplementedException:
The Method or Operation is not implemented
------------------------------

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作,请确保您已将按钮事件处理程序挂接起来

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

  

您说过“虽然按钮事件处理程序已连接”,这是什么意思   通过那个?

如果这是Winforms

按钮

enter image description here

事件

enter image description here

请注意,您也可以在代码中完成

相关问题