题
我有一个小的应用程序,下载了一些文件从一个远程(HTTP)服务器对用户的硬盘驱动器,有些文件是大的,但我不知道是多么大在运行时间。是否有任何方法将允许我下载一个文件中与某些类型的进度表?
这是一个它应用程序,我现在使用Web客户端.DownloadFile()下载的文件。
编辑:我已经看到DownloadProgressChanged和OnDownloadProgressChanged的活动,他们似乎很好地工作,但他们不会的工作为我的解决方案。我下载了几个文件,如果我使用Web客户端.DownloadFileAsync后的事件是称为好几次因为每个文件的呼吁。
这里的基本结构的程序:
- 下载列出的文件通常约为114
- 运行一个循环过列表的文件和下载的每一个其desination
我不介意下载各个文件seperatly但没有下载它们与DownloadFileAsync()我无法使用的事件的处理程序。
解决方案
使用 Web客户端.OnDownloadProgressChanged.记住,这只可能来计算的进步,如果服务器报告的大小。
编辑:
看你的更新,你可以尝试做一个 的队列 的网址。然后,当一个文件完成下载(DownloadDataCompleted 事件),将推出异步下载的下一个网址在队列中。我还没有测试这个。
其他提示
处理Web客户端 DownloadProgressChanged 事件。
我只写了这个和它一定出现以做你想做的。
此外,内,则为ProgressChanged事件已经得到了"TotalBytesToReceive财产"和"BytesReceived"的财产。
private void StartDownload()
{
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Set the progress bar max to 100 for 100%
progressBar1.Value = 0;
progressBar1.Maximum = 100;
// Assign the events to capture the progress percentage
myWebClient.DownloadDataCompleted+=new DownloadDataCompletedEventHandler(myWebClient_DownloadDataCompleted);
myWebClient.DownloadProgressChanged+=new DownloadProgressChangedEventHandler(myWebClient_DownloadProgressChanged);
// Set the Uri to the file you wish to download and fire it off Async
Uri uri = new Uri("http://external.ivirtualdocket.com/update.cab");
myWebClient.DownloadFileAsync(uri, "C:\\Update.cab");
}
void myWebClient_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
void myWebClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
progressBar1.Value = progressBar1.Maximum;
}
我需要解决的一个类似的问题,并GenericTypeTea的代码,如没有招;除了我发现的DownloadDataCompleted事件不是发射的时候你打电话的DownloadFileAsync方法。相反,DownloadFileCompleted事件提出的。