我使用下面的代码从TFS服务器下载多个附件:
foreach (Attachment a in wi.Attachments)
{
WebClient wc = new WebClient();
wc.Credentials = (ICredentials)netCred;
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync(a.Uri, "C:\\" + a.Name);
}
Run Code Online (Sandbox Code Playgroud)
我想使用DownloadFileAsync下载多个文件,但我希望逐个下载它们.
有人可能会问"你为什么不使用同步的DownloadFile方法?" 这是因为:
这是我想到的解决方案:
foreach (Attachment a in wi.Attachments)
{
WebClient wc = new WebClient();
wc.Credentials = (ICredentials)netCred;
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync(a.Uri, "C:\\" + a.Name);
while (wc.IsBusy)
{
System.Threading.Thread.Sleep(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这种方法存在一些问题:
有没有更好的方法使用WebClient.DownloadFileAsync一次下载一个文件?
谢谢!