Webclient.DownloadFileCompleted事件失败

时间:2018-01-11 18:22:33

标签: c# webclient

我正在从网站下载一个大文件。

大小:= 599 MB(629,113,799字节)。

程序运行正常。 但是我遇到了一些错误:

  1. 无法验证文件是否已完全下载。 我没有在屏幕上看到任何显示

    的消息
    Console.WriteLine("..............File succesfully downloaded......This mesage comes from --  wc_DownloadFileCompleted..........");
    
  2. 所以程序会完全下载文件。 (我确实使用网络浏览器手动下载了文件并进行了比较 手动下载和下载的文件之间的文件大小 通过该计划。大小是一样的。所以这对我来说是一个很好的理由 相信程序确实完全下载了文件)

    然而在最后(在显示百分比为99之后) 它会抛出错误..这是消息

      

    WebClient请求期间发生异常。

    错误消息在wc_DownloadFileCompleted内被触发 方法

    所以我需要的帮助是......我们如何验证文件是否已下载 完全(没有任何错误)?我们如何消除错误。

    我是否可以使用其他代码而不是使用代码 我提出了。

  3. 我确实在屏幕上看到了文件下载百分比99。 但是我没有看到" 100"出现。有什么意见吗?

  4. using System.Threading.Tasks;
    using System;
    using System.Data;
    using System.IO;
    using System.IO.Compression;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    using System.Reflection; //Need this to get the DEBUG path
    using MHPUtil;
    using System.Net;
    using System.Globalization;
    
    namespace CmsNpiFileLoad
    {
        public static class FileDownLoadPercentage
        {
            //We need a global variable that will remain the same throughout the run.
            //The unique value for this variable will get set in the DownloadFile() method.
            public static string Value { get; set; }
        }
    
        class CMsNPIFileDownLoad
        {
            //599 MB (629,113,799 bytes) is the size of the file.
    
            public void DownloadCMSNPIFile()
            {
    
                string Destinationfile = "S:\\MIS\\Provider NPI file\\" + "NPI.zip";
                string CmsDownLoadSite = "http://download.cms.gov/nppes/NPPES_Data_Dissemination_January_2018.zip";
    
                //Is the INTERNET AVAILABLE 
                if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
                {
                    Console.WriteLine("Internet available, proceed with the download");
                }
                else
                {
                    Console.WriteLine("Internet not available, proceed with the download");
                    return;
                }
    
                //So at this point we dont have that file with us locally.. so lets download
                Console.WriteLine("Start Downloading....");
                try
                {
                    using (WebClient client = new WebClient())
                    {
                        client.DownloadProgressChanged += wc_DownloadProgressChanged; // This works well 
                        client.DownloadFileCompleted += wc_DownloadFileCompleted;
                        client.DownloadFileAsync(new System.Uri(CmsDownLoadSite), Destinationfile);
                    }
                }
                catch (WebException we)
                {
                    Console.WriteLine(we.ToString());
                }
                Console.ReadLine(); // We dont want the black screen to just disapper from us. so we put a readline so that it will keep displaying all the messsages
    
            }
    
            private static void wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
            {
                if (e.Cancelled)
                {
                    Console.WriteLine("The download has been cancelled");
                    return;
                }
    
                if (e.Error != null)
                {
                    //I tested the the program 2 times.. each time an error occured.
                    //The error message is "An exceptipon occured during a WebClient request.
                    Console.WriteLine("An error ocurred while trying to download file");
                    Console.WriteLine(e.Error.Message.ToString());
                    return;
                }
    
                // I did not see this on the screen.. ????
                Console.WriteLine("..............File succesfully downloaded......This mesage comes from --  wc_DownloadFileCompleted..........");
            }
    
            private static void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
            {
    
                if (FileDownLoadPercentage.Value != e.ProgressPercentage.ToString())
                {
                    FileDownLoadPercentage.Value = e.ProgressPercentage.ToString();
    
                    //Display File Donwload Percentage ( increments of 10 ) 
                    if (Convert.ToInt16(e.ProgressPercentage) % 10 == 0)
                        Console.WriteLine(e.ProgressPercentage.ToString());
    
                    //Show the percentage when it is 99
                    if (Convert.ToInt16(e.ProgressPercentage) == 99)
                        Console.WriteLine(e.ProgressPercentage.ToString());
                }
    
    
                if (e.BytesReceived == e.TotalBytesToReceive)
                {
                    Console.WriteLine("File DownLoad Complete...This message comes from --  wc_DownloadProgressChanged  "); // I did not see this on the screen.. ????
                }
            }
        }
    }
    

    Console application written in C#

0 个答案:

没有答案