简体   繁体   English

如何确保我下载的文件已完成?

[英]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.

相关问题 我如何 %100 确定已在 C# 中完成了对文本文件的写入,以便我可以继续执行其他操作? - How can I be %100 sure that writing to a text file has been finished in C#, so that I can proceed with another operation? 如何确保C#中的文件路径安全? - How do I make sure a file path is safe in C#? 我可以在创建实例之前确保静态构造函数完成吗? - Can I make sure a static constructor has finished before I create an instance? c#如何确保Webclient留有必要的下载时间 - c# How Do I make Sure Webclient allows necessary time for download 当代码未完成时,如何在事件发生时如何更新文本框? - How do I make a textbox update as an event happens not when the code has finished? 如何确保每个选定的单元格具有相同的值? - How do I make sure each selected cell has same value? 如何在访问任何页面之前检查以确保客户已完成任务 - How do I check to make sure the customer has done a task before they visit any page 如何确保DirectDraw表面具有正确的文件格式? - How can I make sure that a DirectDraw surface has a correct file format? 在 Unity C# 中,文件下载完成后如何调用方法? - How Do I Call a Method After a File Has Finished Downloading In Unity C#? 我怎样才能确保 FirstOrDefault<KeyValuePair> 已返回值 - How can I make sure that FirstOrDefault<KeyValuePair> has returned a value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM