C# Code Snippet - Download File from URL

C# Code Snippet - Download File from URL

C# Code Snippet - Download File from URL

(C-Sharp) C# code snippet download file from URL and save it on local drive. These code snippets discuss both asynchronous file download method and DownloadFile method (to block thread while waiting for the download).

Bookmark:

C# Code Snippet - Download File from URL

This .Net C# code snippet download file from URL to local disk. To use this function simply provide the URL of the file and file name to save on local drive. This function uses System.Net namespace to download the file.

/// <summary>
/// Function to download a file from URL and save it to local drive
/// </summary>
/// <param name="_URL">URL address to download file</param>
public void DownloadFile(string _URL, string _SaveAs)
{
    try
    {
        System.Net.WebClient _WebClient = new System.Net.WebClient();
        // Downloads the resource with the specified URI to a local file.
        _WebClient.DownloadFile(_URL, _SaveAs);
    }
    catch (Exception _Exception)
    {
        // Error
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
    }
}


Here is a simple example showing how to use above function (DownloadFile) to download a file and save it on a local disk.

// Download file from URL
DownloadFile("http://www.yourdomain.com/sample-file.zip", @"C:\sample-file.zip");


Download File Asynchronously
To download a file without blocking main calling thread, Downloads, to a local file, the resource with the specified URI. This method does not block the calling thread.

To receive notification when the file is available, add an event handler to the DownloadFileCompleted event. This event occurs when an asynchronous file download operation completes. To update status/progress bar while downloading file use DownloadProgressChanged event. This event occurs when an asynchronous download operation successfully transfers some or all of the data. You can use the CancelAsync method to cancel asynchronous operations that have not completed.

To block while waiting for the download to complete, use one of the DownloadFile (discuss in above) methods.

// Occurs when an asynchronous file download operation completes.
private void _DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    // File download completed
    download_button.Enabled = Enabled;
    MessageBox.Show("Download completed");
}

// Occurs when an asynchronous download operation successfully transfers some or all of the data.
private void _DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
    // Update progress bar
    progressBar1.Value = e.ProgressPercentage;
}


// download button click event
private void download_button_Click(object sender, EventArgs e)
{
    // Disable download button to avoid clicking again while downloading the file
    download_button.Enabled = false;

    // Downloads, to a local file, the resource with the specified URI. 
    // This method does not block the calling thread.
    System.Net.WebClient _WebClient = new System.Net.WebClient();
    _WebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(_DownloadFileCompleted);
    _WebClient.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(_DownloadProgressChanged);
    _WebClient.DownloadFileAsync(new Uri("http://www.youdomain.com/sample-file.zip"), @"C:\sample-file.zip");
}


C# Keywords Used:

  • WebClient
  • DownloadFile
  • DownloadFileCompleted
  • DownloadProgressChanged
  • DownloadFileAsync
  • Exception

Code Snippet Information:

  • Applies To: .Net, C#, CLI, Download file, WebClient, DownloadFile, DownloadFileAsync, DownloadFileCompleted, DownloadProgressChanged, Asynchronous file download
  • Programming Language : C# (C-Sharp)

External Resources:

Leave a comment