[英]How do I make sure that the file I download has finished?
I have this application which allows me to download certain files from the internet when I press a button. 我有这个应用程序,当我按下按钮时,我可以从互联网上下载某些文件。 However.. I have no idea how to make sure that the file has actually finished downloading.
但是..我不知道如何确保该文件实际上已完成下载。
I'm currently using the good old WebClient method of downloading files 我目前正在使用旧的WebClient方法下载文件
using (WebClient client = new WebClient())
{
client.DownloadFileAsync(new System.Uri(requestUrl), combinedPaths);
}
There is no need to show more of the code really since the download function is very simple and straight forward. 由于下载功能非常简单直接,因此无需显示更多代码。 I need to figure out how to keep track of the file that you are downloading to see how much it has downloaded for example 1 / 100%.. I have a very good ISP with 250 Mb/s download speed so its really hard for me to see if it has actually been finished or not.
我需要弄清楚如何跟踪你正在下载的文件以查看它下载了多少例如1/100%..我有一个非常好的ISP,下载速度为250 Mb / s所以对我来说真的很难看看它是否真的已经完成。
You can fire WebClient.DownloadFileCompleted
event and do something with it: 您可以触发
WebClient.DownloadFileCompleted
事件并对其执行某些操作:
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallback);
To know the status of the download you can subscribe to another event handler: 要了解下载的状态,您可以订阅另一个事件处理程序:
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
The WebClient has an event handler called DownlaodFileCompleted that you can use to trigger when the file has finished downloading : WebClient有一个名为DownlaodFileCompleted的事件处理程序,您可以使用它在文件下载完成时触发:
myWebClient.DownloadFileCompleted += DownloadCompleted;
public static void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
{
Console.WriteLine("Success");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.