Coder Perfect

In C#, how do you download a file from a URL?


What’s the simplest way to get a file from a URL path?

Asked by vbroto

Solution #1

using (var client = new WebClient())
    client.DownloadFile("", "a.mpeg");

Answered by Raj Kumar

Solution #2

Include this namespace

using System.Net;

Download asynchronously and place a ProgressBar within the UI Thread to show the download status.

private void BtnDownload_Click(object sender, RoutedEventArgs e)
    using (WebClient wc = new WebClient())
        wc.DownloadProgressChanged += wc_DownloadProgressChanged;
        wc.DownloadFileAsync (
            // Param1 = Link of file
            new System.Uri(""),
            // Param2 = Path to save
// Event to track the progress
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    progressBar.Value = e.ProgressPercentage;

Answered by Abdul Saleem

Solution #3

Use System.Net.WebClient.DownloadFile:

string remoteUri = "";
string fileName = "ms-banner.gif", myStringWebResource = null;

// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
    myStringWebResource = remoteUri + fileName;
    // Download the Web resource and save it into the current filesystem folder.
    myWebClient.DownloadFile(myStringWebResource, fileName);        

Answered by vbroto

Solution #4

using System.Net;

WebClient webClient = new WebClient();
webClient.DownloadFile("", @"c:\myfile.txt");

Answered by petrzjunior

Solution #5

Complete the class in order to download a file and print the status to the console.

using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Threading;

class FileDownloader
    private readonly string _url;
    private readonly string _fullPathWhereToSave;
    private bool _result = false;
    private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);

    public FileDownloader(string url, string fullPathWhereToSave)
        if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
        if (string.IsNullOrEmpty(fullPathWhereToSave)) throw new ArgumentNullException("fullPathWhereToSave");

        this._url = url;
        this._fullPathWhereToSave = fullPathWhereToSave;

    public bool StartDownload(int timeout)

            if (File.Exists(_fullPathWhereToSave))
            using (WebClient client = new WebClient())
                var ur = new Uri(_url);
                // client.Credentials = new NetworkCredential("username", "password");
                client.DownloadProgressChanged += WebClientDownloadProgressChanged;
                client.DownloadFileCompleted += WebClientDownloadCompleted;
                Console.WriteLine(@"Downloading file:");
                client.DownloadFileAsync(ur, _fullPathWhereToSave);
                return _result && File.Exists(_fullPathWhereToSave);
        catch (Exception e)
            Console.WriteLine("Was not able to download file!");
            return false;

    private void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        Console.Write("\r     -->    {0}%.", e.ProgressPercentage);

    private void WebClientDownloadCompleted(object sender, AsyncCompletedEventArgs args)
        _result = !args.Cancelled;
        if (!_result)
        Console.WriteLine(Environment.NewLine + "Download finished!");

    public static bool DownloadFile(string url, string fullPathWhereToSave, int timeoutInMilliSec)
        return new FileDownloader(url, fullPathWhereToSave).StartDownload(timeoutInMilliSec);


static void Main(string[] args)
    var success = FileDownloader.DownloadFile(fileUrl, fullPathWhereToSave, timeoutInMilliSec);
    Console.WriteLine("Done  - success: " + success);

Answered by Jonas_Hess

Post is based on