Click here to Skip to main content
Click here to Skip to main content

Download File Using C#

By , 30 May 2012
Rate this:
Please Sign up or sign in to vote.

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

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)

About the Author


Comments and Discussions

 
GeneralMy vote of 5 PinmemberChan Nyein12-Apr-14 16:54 
QuestionLike it PinmemberSiva Hyderabad27-Feb-14 18:17 
Questionhello PinmemberRahim Lotfi19-Feb-14 10:14 
QuestionDownload multiple files to a folder from a url Pinmembermehrarohit12-Feb-14 18:20 
GeneralMy vote of 4 Pinmemberali yeganeh31-Oct-12 9:51 
GeneralRe: My vote of 4 PinmemberMember 93199625-Feb-13 6:44 
GeneralMy vote of 4 Pinmemberkishore doni28-Aug-12 0:57 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 30 May 2012
Article Copyright 2012 by tejasbhalani
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid