[英]passing filename to DownloadFileCompleted in async file download using webClient C#
我的程序正在使用隊列,以異步方式使用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(最好是參數中的字符串)。
使用重載方法WebClient.DownloadFileAsync(Uri address, string fileName, object userToken)
,可以將文件名作為userToken傳遞,然后在DownloadFileCompleted處理程序中訪問它。
userToken: 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.