Download file from url with C#

by on Mar.01, 2013, under C#

In the following code we are going to use WebClient class to download a file from a web location.

public void DownloadFile(String URL){
    Uri uri = new Uri(URL);
    WebClient wb = new WebClient();
    wb.DownloadFileCompleted += new AsyncCompletedEventHandler(completed);
    wb.DownloadFileAsync(uri, System.IO.Path.GetTempPath() + Path.GetFileName(uri.LocalPath));
}
 
private void completed(object sender, AsyncCompletedEventArgs e)
{
    try
    {
        Console.WriteLine("Download completed");
    }
    catch (Exception ex)
    {
    	Console.WriteLine(ex.Message);
    }
}
:, ,

%d bloggers like this: