0

i'm writing an application that downloading .xml files from our server, and retreive some data from them.

The file are data from parts we are using in my Department.

Some of files are 0Kb, meaning we don't have data from this part.

Now my problem is that the Webclient DownloadFileCompleted event is not firing when finishing downloading the 0Kb file.

Here's my code:

    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;
    }

Thank you for your help.

dadou
  • 45
  • 1
  • 11
  • 1
    What [http status code](https://httpstatuses.com/) is the webserver returning when there is a 0kb file? (Like 200, 204 or 404)? – wimh Oct 02 '19 at 07:59
  • Maybe a better approach to handle 0kb files differently, since there is no need to download them (and downloading may never be "completed" in that scenario). – Stefan Oct 02 '19 at 11:14
  • Is it downloading the file and saving to disk anyhow? At first glance your code looks pretty similar to that in this: https://stackoverflow.com/questions/33720201/c-sharp-webclient-downloads-empty-file In which case flag and check if the file is there on disk anyhow if downloadcompleted doesn't fire. – Andy Oct 02 '19 at 11:54
  • @Stefan - I'm cleaning them after, the problem is that i can't download them, verify and delete them, i'm getting every time that the file is already used. – dadou Oct 03 '19 at 05:24
  • @Andy - Yes the file is saving to disk. I found a solution to increase the progress bar without the Webclient DownloadFileCompleted event. – dadou Oct 03 '19 at 05:26

0 Answers0