How to Download Files With C# from a Web Server

How to use C# to manage Internet communications and download files with C# or collect information and data from web services.

By Tim TrottC# ASP.Net MVC • June 26, 2010
How to Download Files With C# from a Web Server

Microsoft Dot Net framework provides a set of classes that manage Internet communications, and one in particular can be used to download files with C# from the Internet to the local hard drive. This can be used to download a data set, for example, or to download program updates.

There are two methods for downloading a file with C#, synchronously and asynchronously.

Synchronous requests execute sequentially, blocking further code execution until the request is completed, which can lead to slower performance. Asynchronous requests allow code to continue executing while waiting for a response, improving overall responsiveness and efficiency, especially in web applications. Asynchronous programming is often preferred for tasks like fetching data from servers or performing I/O operations, where waiting for a response should not hinder other processes.

Synchronous Download Files With C#

A Synchronous download will only allow one download to be processed. While it is being processed, the main program thread will pause until the download is complete. It is the simplest method to implement, and if you can wait for the download to complete, it is often the best.

C#
using System.Net;

using(WebClient fileDownloader = new WebClient())
{
  fileDownloader.DownloadFile("https://lonewolfonline.net/robots.txt", "c:\robots.txt");
}

Asynchronous Download Files With C#

An asynchronous download does not block the main program thread apart from the initial DNS lookup (converting a hostname to an IP address). Using an IP address directly can avoid this delay. This method is more involved, but if you cannot wait for the download to finish or need to download a large file, this is the best method.

C#
using System.Net;

using(WebClient fileDownloader = new WebClient())
{
  fileDownloader.DownloadFileAsync("https://lonewolfonline.net/robots.txt", "c:\robots.txt");
}

The Asynchronous Download method also contains a few events that can be used for progress bars and notifications, as well as an event that is triggered on completion of the download.

C#
...
fileDownloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(updateProgress);
...

private void updateProgress(object sender, DownloadProgressChangedEventArgs Arg)
{
  progressBar.Value = Arg.ProgressPercentage;
}

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.