进度条和webclient

时间:2010-11-13 10:59:36

标签: c# download progress-bar webclient

我有一个大约10-30秒的事件,即从页面下载信息(流量很大),修改它然后使用WebClient将其保存到磁盘上。因为它花了这么长时间,我想添加一个进度条或制作一个更新标签(比如说更新......)以表明进度。

有人可以指导我如何做到这一点吗?我可以用WebClient来处理这个事件吗?

1 个答案:

答案 0 :(得分:19)

如果您正在编写Windows窗体客户端应用程序(而不是ASP.NET服务器端组件),则可以按如下方式显示WebClient下载的进度:

WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += (s, e) =>
{
    progressBar.Value = e.ProgressPercentage;
};
webClient.DownloadFileCompleted += (s, e) =>
{
    progressBar.Visible = false;
    // any other code to process the file
};
webClient.DownloadFileAsync(new Uri("http://example.com/largefile.dat"),
    @"C:\Path\To\Output.dat");

progressBar是表单上ProgressBar对象的ID。)

相关问题