一次下载多个文件

时间:2014-03-23 20:26:24

标签: c#

这是下面的代码......

using System.Net;

   //Create Temporary Folder to Holde All the Downloads............
    namespace downloadFileCSharp10
   {
    public partial class Form1 : Form
    {

      string url = "URL";
      string path = @"c:\Folder";
      public Form1()
      {
        InitializeComponent();
      }

      void InitiateDownload(string RemoteAddress, string LocalFile, AsyncCompletedEventHandler CompleteCallBack, object userToken)
    {
        WebClient wc = new WebClient();
        wc.DownloadProgressChanged += wc_DownloadProgressChanged;
        wc.DownloadFileCompleted += wc_DownloadFileCompleted;
        wc.DownloadFileAsync(new Uri(RemoteAddress), LocalFile, userToken);



    }

    void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {

        if (e.Error != null)
        {
            lblInfo1.Visible = true;
            lblInfo1.ForeColor = Color.Red;
            lblInfo1.Text = "Error Downloading ";

            //throw e.Error;
        }
        else if (e.Cancelled)
        {
            lblInfo1.Visible = true;
            lblInfo1.ForeColor = Color.Red;
            lblInfo1.Text = "Download Cancelled " + e.UserState + e.Error;
        }
        else
        {
            lblInfo1.Visible = true;
            lblInfo1.ForeColor = Color.Red;
            lblInfo1.Text = e.UserState + " Download Complete!! ";
        }
        //throw new NotImplementedException();
    }

    void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        double bytesIn = double.Parse(e.BytesReceived.ToString());
        double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
        double percentage = bytesIn / totalBytes * 100;
        progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
        label1.Text = e.ProgressPercentage.ToString() + "%";


        //throw new NotImplementedException();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btnGetDownload_Click(object sender, EventArgs e)
    {

                  //First Download
                 InitiateDownload(URL, path + "name you give.txt", wc_DownloadFileCompleted, "name you give.txt");
                 //Second Download
                 InitiateDownload(URL, path + "name you give2.txt", wc_DownloadFileCompleted, "name you give2.txt");

    }


     }
  }

当我下载多个文件时,它会同时下载所有文件,并且我会不时下载一两个已损坏的下载文件。此外,当我有超过10或15个我的应用程序冻结了一点,因为它下载全部相同。我希望能够一次至少下载一到两个。我已经环顾四周使用异步和等待,但没有运气。另外我想为每个下载文件放一个进度条。所以基本上是一个循环,但插入到listview但不知道如何去做或者可能如何处理它。

1 个答案:

答案 0 :(得分:0)

您可以查看http://blogs.msdn.com/b/spike/archive/2013/06/12/how-to-download-multiple-files-concurrently-using-webclient-and-the-downloadfileasync-method.aspx

据我了解,您应将同时传出连接限制配置为更高的数字。

在项目的App.config文件中,添加maxconnection设置:

<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>

    <system.net>
      <connectionManagement>
        <addaddress = "*"maxconnection = "10" />
      </connectionManagement>
    </system.net>

</configuration>

对上层链接的信用。但这只能解释当请求大量文件时冻结。对于文件损坏,我认为还有另一点需要解决。