|
Code Sample: Programmatically Download File Using C#
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.thereforesystems.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
27132 views, 11 so far
today |
Follow Discussion
16 Responses to “Code Sample: Programmatically Download File Using C#”
Leave a Reply
Get Updates By Email
Popular Post
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Free Icons And Images With Visual Studio 2008
- Windows 7 Control Panel In Classic Mode
- Dynamic Sort With LINQ
- Use SqlConnection With LINQ To SQL
- StyleCop Tutorial
- Write To Vista Event Log Using C#
- More Details Emerge On Microsoft Master Certification
Tag Cloud
Code Snippets
- Get Current Windows User In C#
- Get Width And Height Of Image In C#
- Get Windows Registry Size With WMI And C#
- Reverse Array Elements Using C#
- Convert Hexadecimal To Number In C#
- Get Free Disk Space Using T-SQL
- SQL Server 2008 – Get All Indexes In A Database
- Get Name Of Current Executing Assembly In C#
- Get CD Or DVD Drive Information Using WMI And C#
- Get Last Row From Table Using LINQ To SQL


March 3rd, 2009 at 4:31 pm
Hello! Thank you for the post.
I am anxious to know if this will work on computer behind PROXY?
March 3rd, 2009 at 4:35 pm
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.
March 3rd, 2009 at 4:38 pm
Thank dude for a quick reply!
I’ll try this.
Good Day!
June 13th, 2009 at 9:08 pm
Hello! Thank you for the post.
June 14th, 2009 at 10:06 am
Bikash,
Thank you for your comment.
August 27th, 2009 at 3:30 pm
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
August 27th, 2009 at 3:34 pm
Hi Himanshu,
It should work fine with any file extension.
August 27th, 2009 at 4:07 pm
HI,
thx for the quick reply
i’ll just check and report back
thx again.
November 21st, 2009 at 9:18 pm
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..
January 24th, 2010 at 9:27 pm
aha … thanks for help
April 24th, 2010 at 5:06 pm
The code is really helpful, but i Need one clarification
I want to download a file from the web and the url is the indirect url (there is no filename in the url it has query param), so code does not know about the file name, how can i get the file name
I ran through few stuffs for indirect url using webclient downloadfile method.
I have get filename in content-dispostion or content-location of the response headers.
But some times the response header does not have content-type or content-disposition (to get file name) in it, in this situation what can i do here to get the real file name?
Any help will be appreciated from this end.
April 26th, 2010 at 2:23 pm
Thanx dude
July 1st, 2010 at 4:12 am
Thanks for the code.
I am trying to write a program that will be scheduled to run everyday to download a file from an ftp site. The file is created automatically everyday and saved to the ftp site by another program. The file is named abc_datetimestamp.txt. Since the file name contains the create timestamp, the file name changes everyday. Therefore I do not know what the exact file name will be. Is there any way I can use the WebClient.DownloadFile method without knowing the complete file name? Or is there a way I could retrieve the file name from the ftp site and then feed it to the webclient method? Please note that the file is deleted after it is downloaded from the ftp site, so at any one time, there will be only one file that starts with abc.
Any help will be appreciated.
July 1st, 2010 at 11:49 am
Rajib,
Your best bet will be to transfer the file via ftp. You can download all files in a folder this way. There are many ftp libraries available for .NET both commercial and open source.
August 26th, 2010 at 4:10 pm
If network goes down while downloading file, then downloading will break. If I am downloading a big file, then is there any way so that file downloading can continue from same point???
August 26th, 2010 at 4:38 pm
Raja,
If downloading the file programmatically is not a strict requirement then you can use a tool such as http://www.freedownloadmanager.org/
The tool is superb and incorporates features such as resume support, scheduler and much more.