.NET WebClient标记属性,还是跟踪下载?

时间:2010-03-08 00:20:25

标签: c# download webclient

我正在尝试使用

从Web服务器实现异步文件下载
  private void btnDownload_Click(object sender, EventArgs e)
        {
            WebClient webClient = new WebClient();
            webClient.Credentials = new NetworkCredential("test", "test");
            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
            webClient.DownloadFileAsync(new Uri("ftp://2.1.1.1:17865/zaz.txt"), @"c:\myfile.txt");
        }

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

        private void Completed(object sender, AsyncCompletedEventArgs e)
        {

            MessageBox.Show("Download completed!");
        }

现在下载工作正常,我想知道的是: 假设我一次下载超过1个,那么分别跟踪每个下载和报告进度的最佳方法是什么? 我正在寻找WebClient的标签属性,但找不到。

谢谢

1 个答案:

答案 0 :(得分:3)

您可以将第三个参数传递给DownloadFileAsync(userToken)。它可以是您想要的任何内容,您可以在DownloadProgressChanged事件参数(UserState属性)中检索它。因此,您只需要传递URI或一些唯一ID作为用户令牌,以便您可以识别进度已更改的下载