C# WebClient未下载完整文件?

C# WebClient未下载完整文件?,c#,webclient,downloadfileasync,C#,Webclient,Downloadfileasync,我有一个WebClient下载一个.chm文件(如下面的代码所示)。它下载的内容似乎非常不规则。完整文件大小约为2500-2600 KB,但在我返回的文件中,大约有50-75%的时间是较小的(例如:1233 KB、657 KB、353 KB、1745 KB等)。 (代码已简化/个人详细信息已删除) 和事件,这是工作正常,但在一个“未完成”的文件: 我真的错过了什么吗?看起来保罗·鲁布的评论让我找到了正确的解决方案。我只是在检查e.Cancelled属性 偶尔会抛出异常(主要是某种类型的WebEx

我有一个
WebClient
下载一个.chm文件(如下面的代码所示)。它下载的内容似乎非常不规则。完整文件大小约为2500-2600 KB,但在我返回的文件中,大约有50-75%的时间是较小的(例如:1233 KB、657 KB、353 KB、1745 KB等)。
(代码已简化/个人详细信息已删除)

和事件,这是工作正常,但在一个“未完成”的文件:


我真的错过了什么吗?

看起来保罗·鲁布的评论让我找到了正确的解决方案。我只是在检查
e.Cancelled
属性


偶尔会抛出异常(主要是某种类型的
WebException
),导致下载出错。一旦我检查了
e.Error
属性,我就可以根据自己的情况重试或处理失败。

您是否正在检查
AsyncCompletedEventArgs e
的内容,尤其是
取消的
错误
属性?啊!我只是在检查
取消了
。看起来
Error
返回时出现了某种异常(我必须进一步调查)。
public static void DownloadMyFile(string destFileAndPath)
{
    //Get base for help Url, stop at first "/" ignoring "https://", then add path in server
    string Url = "https://mywebservice.com/myFile.chm";

    using (var client = new WebClient())
    {
        //I need to do stuff to the downloaded file when done
        client.DownloadFileCompleted += client_DownloadFileCompleted;

        client.DownloadFileAsync(new Uri(Url), destFileAndPath);

        //More waiting?
        while (client.IsBusy) { }
    }
}
public static void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    //Do stuff like comparing the file to another, renaming, copying, etc.
}