【问题标题】:passing filename to DownloadFileCompleted in async file download using webClient C#使用 webClient C# 在异步文件下载中将文件名传递给 DownloadFileCompleted
【发布时间】:2014-04-04 14:29:55
【问题描述】:

我的程序正在使用队列以使用 webClient 的异步方法一一下载文件列表。 它看起来像这样:

    public void DownloadFile()
    {
        if (_downloadUrls.Any())
        {
            var urlAddress = _downloadUrls.Dequeue();
            //Irrelevant code that gets correct URL, and location from queue _downloadUrls

            try
            {
                // Start downloading the file
                webClient1.DownloadFileAsync(URL, location);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        else
        {
            MessageBox.Show("complete!");
        }
    }

这是我的 DownloadFileCompleted 代码:

private void webClient1_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        if (e.Cancelled == true)
        {
            // MessageBox.Show("Download has been canceled.");
        }
        else
        {
            DownloadFile();
        }
    }

问题是如何将有关文件名的信息传递给 DownloadFileCompleted? 我想更改下载文件的最后访问日期,以便它们与服务器上的相同,我只能在 webClient1_DownloadFileCompleted 中执行此操作,但我不知道哪个文件触发了事件 DownloadFileCompleted。如何将此信息传递给 DownloadFileCompleted(最好作为参数中的字符串)。

【问题讨论】:

    标签: c# queue webclient


    【解决方案1】:

    使用重载方法WebClient.DownloadFileAsync(Uri address, string fileName, object userToken),可以将文件名作为userToken传递,然后在DownloadFileCompleted handler中访问。

    用户令牌: A user-defined object that is passed to the method invoked when the asynchronous operation completes.

    http://msdn.microsoft.com/en-us/library/ms144197(v=vs.110).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      相关资源
      最近更新 更多