Click here to Skip to main content
15,896,207 members
Articles / Programming Languages / C#
Tip/Trick

Download File Using C#

Rate me:
Please Sign up or sign in to vote.
4.50/5 (17 votes)
30 May 2012CPOL 208.4K   15   12
Download file using C#.

Introduction

This article describes code to download a file using C#...

Background

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.

Using the code

C#
private void buttonDownloadFile_Click(object sender, EventArgs e)
{
    string url = @"http://www.thereforesystems.com/wp-content/uploads/2008/08/image35.png";
    // Create an instance of WebClient
    WebClient client = new WebClient();
    // Hookup DownloadFileCompleted Event
    client.DownloadFileCompleted +=    new AsyncCompletedEventHandler(client_DownloadFileCompleted);

    // Start the download and copy the file to c:\temp
    client.DownloadFileAsync(new Uri(url), @"c:\temp\image35.png");
 }

void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    MessageBox.Show("File downloaded");
}

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionDownload file Pin
Member 1394826017-Aug-18 1:33
Member 1394826017-Aug-18 1:33 
QuestionDownload files from HTTPS Pin
MohamedImtiyaz.net10-Feb-15 3:25
MohamedImtiyaz.net10-Feb-15 3:25 
Good Day,

I need some assistance, I have been researching endlessly on every search engine known.
I have a scenrio website, where the user logs in to the site (webform).
then navigates to a set of links each link represents an xml file.

I wish for my application to automatically download this file and save it to a location on my hardrive.

I found and tested the following lines of code:

WebClient wbClient = new WebClient();

wbClient.Credentials = new NetworkCredential(strUserName, strPassword, strDomain);
wbClient.DownloadFileAsync(strFileNameToDownload, strLocationToSave);




This works fine if the domain is HTTP.
the moment the domain is https://mysite.com/file.xml
i either get the following scenarios:

1. the file downloads BUT the file is EMPTY with ZERO kb file size.
OR
2. an error 407 proxy server authentication required.

your assistance / feedback to guide me in the right direction is greatly appreciated.
NewsDownload file from secure folder Pin
jayprakash sir ji2-Dec-14 1:17
professionaljayprakash sir ji2-Dec-14 1:17 
GeneralMy vote of 1 Pin
Member 1079524529-Sep-14 2:54
Member 1079524529-Sep-14 2:54 
Questionregarding youtube Pin
aman bhardwaj9-Sep-14 21:51
aman bhardwaj9-Sep-14 21:51 
GeneralMy vote of 5 Pin
Chan Nyein12-Apr-14 16:54
Chan Nyein12-Apr-14 16:54 
QuestionLike it Pin
Siva Hyderabad27-Feb-14 18:17
Siva Hyderabad27-Feb-14 18:17 
Questionhello Pin
Rahim Lotfi19-Feb-14 10:14
Rahim Lotfi19-Feb-14 10:14 
QuestionDownload multiple files to a folder from a url Pin
mehrarohit12-Feb-14 18:20
mehrarohit12-Feb-14 18:20 
GeneralMy vote of 4 Pin
ali yeganeh31-Oct-12 9:51
ali yeganeh31-Oct-12 9:51 
GeneralRe: My vote of 4 Pin
Shaharyar Ahmed5-Feb-13 6:44
Shaharyar Ahmed5-Feb-13 6:44 
GeneralMy vote of 4 Pin
D-Kishore28-Aug-12 0:57
D-Kishore28-Aug-12 0:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.