Home / Programming / Blog article: Code Sample: Programmatically Download File Using C#

| RSS

Code Sample: Programmatically Download File Using C#

September 17th, 2008 | 10 Comments | Posted in Programming

While browsing forums today I came across a question which asked for a solution to download a file from a web server programmatically. The solution is very simple and below is the code which achieves the goal. Here I am downloading a file asynchronously on Button Click.


   1: private void buttonDownloadFile_Click(object sender, EventArgs e)
   2: {
   3:     string url
   4:         = @"http://www.onedotnetway.com/wp-content/uploads/2008/08/image35.png";
   5:
   6:     // Create an instance of WebClient
   7:     WebClient client = new WebClient();
   8:
   9:     // Hookup DownloadFileCompleted Event
  10:     client.DownloadFileCompleted +=
  11:         new AsyncCompletedEventHandler(client_DownloadFileCompleted);
  12:
  13:     // Start the download and copy the file to c:\temp
  14:     client.DownloadFileAsync(new Uri(url), @"c:\temp\image35.png");
  15: }
  16:
  17: void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  18: {
  19:     MessageBox.Show("File downloaded");
  20: }

You can also download the file synchronously using WebClient.DownloadFile() method.






Leave a Reply 16482 views, 23 so far today |
Tags: ,
Follow Discussion

10 Responses to “Code Sample: Programmatically Download File Using C#”

  1. S.P.I.D.E.R.M.A.N Says:

    Hello! Thank you for the post.

    I am anxious to know if this will work on computer behind PROXY?

  2. Deepak Says:

    Thanks. If it does not work then you can set WebClient.UseDefaultCredentials. This should do the trick. And if you have to specify credentials for your proxy explicitly then you can set WebClient.Credentials property.

    Hope this helps.

  3. S.P.I.D.E.R.M.A.N Says:

    Thank dude for a quick reply!
    I’ll try this.
    Good Day!

  4. Bikash Says:

    Hello! Thank you for the post.

  5. Deepak Says:

    Bikash,

    Thank you for your comment.

  6. HIMANSHU Says:

    Hi,
    I need to know if this Code works Successfully for Downloading a file with .pdf extension on my local system drive I’hv tried a similar but failed to do so

    I need to download a file opened in a browser with .pdf extension

    Plz Help
    Help Would Be Appreciated
    Himanshu

  7. Deepak Says:

    Hi Himanshu,

    It should work fine with any file extension.

  8. HIMANSHU Says:

    HI,
    thx for the quick reply
    i’ll just check and report back
    thx again.

  9. nikeeta Says:

    hi all.. very use full article but.. can we use it in web aplication.. on internet.. where ever i get example of WebClient , i get it in window application, in web application, is it possible to add event handler “DownloadDataCompletedEventHandler” to track download complete ? any help on this will be appreciated.. thank u..

  10. ArabicGeek Says:

    aha … thanks for help

Leave a Reply