我使用以下代码从链接下载文件:
WebClient webClient = new WebClient();
webClient.Headers.Add("Accept: text/html, application/xhtml+xml, */*");
webClient.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
webClient.DownloadFileAsync(new Uri(downloadURL), "C:\\Users\\" + Environment.UserName + "\\Documents\\AudioStreamerUpdate_" + rnd1.ToString() + ".zip");
//track the downloading progress
webClient.DownloadProgressChanged += (sender, e) =>
{
progressBar1.Value = e.ProgressPercentage;
label1updateinf.Text = e.ProgressPercentage + "%";
Console.WriteLine(e.ProgressPercentage + "%");
};
由于文件大小约为200 Mb,我希望跟踪下载进度。
我也试过这个代码:
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressUpdate);
private void webClient_DownloadProgressUpdate(object sender, DownloadProgressChangedEventArgs e)
{
}
但它给了我一个错误
"DownloadProgressCallback"没有与委托"DownloadProgressChangedEventHandler"匹配的重载
1条答案
按热度按时间t5zmwmid1#
如果在调用download方法 * 之前 * 附加事件处理程序,则代码应该可以工作: