我使用下面的代码从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一次下载一个文件?
谢谢!
我需要这个代码的帮助
#region Events
public class DownloadProgressChangedEventArg
{
private long _ProgressPercentage;
private long _BytesReceived;
private long _TotalBytesToReceive;
public DownloadProgressChangedEventArg(long BytesReceived, long TotalBytesToReceive)
{
_BytesReceived = BytesReceived;
_TotalBytesToReceive = TotalBytesToReceive;
_ProgressPercentage = BytesReceived * 100 / (TotalBytesToReceive);
}
public long BytesReceived { get { return _BytesReceived; } set { _BytesReceived = value; } }
public long ProgressPercentage { get { return _ProgressPercentage; } set { _ProgressPercentage = value; } }
public long TotalBytesToReceive { get { return _TotalBytesToReceive; } set { _TotalBytesToReceive …
Run Code Online (Sandbox Code Playgroud)