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

| RSS

Code Sample: Programmatically Download File Using C#

September 17th, 2008 | No 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.


Get daily updates via RSS


Leave a Reply 724 views, 8 so far today |
Tags: ,

Leave a Reply


View of Sydney from WavertonView of Sydney from WavertonView of Sydney from WavertonView of Sydney from WavertonView of Sydney from WavertonHarbour Bridge and Opera HouseHarbour Bridge and Opera HouseIMAGE_00018IMAGE_00017IMAGE_00016