Webclient downloadfileasync not working

Sandeep Bansal picture Sandeep Bansal · Mar 23, 2010 · Viewed 8.7k times · Source

I got a WPF application and I want to download a file.

I'm using System.Net; and I have the following code:

WebClient ww = new WebClient();
ww.DownloadFileAsync(
    new Uri("http://www.sinvise.net/tester/1.jpg"), 
    AppDomain.CurrentDomain.BaseDirectory + "\\1.jpg");

The problem is, is that it doesn't download the file, it's just showing up as 0kb file and not downloading, I don't know what the problem is, can anyone help?

Answer

spender picture spender · Mar 23, 2010

How about listening for the DownloadFileCompleted event and checking the AsyncCompletedEventArgs.Error property the event forwards to your handler?

    public static void DownLoadFileInBackground(string address)
    {
        WebClient client = new WebClient();
        Uri uri = new Uri(address);
        client.DownloadFileCompleted += (sender,e)=>
                                        {
                                            //inspect e here:
                                            //e.Error
                                        };
        client.DownloadProgressChanged += (sender,e)=>
                                          {
                                              //e.ProgressPercentage
                                          };
        client.DownloadFileAsync(uri, "blabla");
    }