首页 > 解决方案 > WebClient 不报告 0Kb 文件

问题描述

我正在编写一个从我们的服务器下载 .xml 文件并从中检索一些数据的应用程序。

该文件是我们在我部门使用的零件的数据。

有些文件是 0Kb,这意味着我们没有这部分的数据。

现在我的问题是完成下载 0Kb 文件时 Webclient DownloadFileCompleted 事件没有触发。

这是我的代码:

    private void Get_Tool_Model()
    {
        if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
        {
            download_Tool_Model = true;
            using (WebClient wc = new WebClient())
            {
                wc.UseDefaultCredentials = true;
                wc.DownloadProgressChanged += (o, e) =>
                {
                    double bytesIn = double.Parse(e.BytesReceived.ToString());
                    double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
                    double percentage = bytesIn / totalBytes * 100;
                    Dispatcher.Invoke(() => {
                        txt_progress.Text = "Downloaded " + e.BytesReceived + " of " + e.TotalBytesToReceive;
                        progress.Value = e.ProgressPercentage;
                    });
                };
                wc.DownloadFileCompleted += new AsyncCompletedEventHandler(Wc_DownloadFileCompleted);
                wc.DownloadFileAsync(
                    // Param1 = Link of file
                    new System.Uri(path),
                    // Param2 = Path to save
                    "C:\\Tool_Model.xml"
                );
            }
        }
    }

    private void Wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        download_completed = true;
    }

谢谢您的帮助。

标签: c#wpf

解决方案


推荐阅读