Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to download a file from a URL in C#?
A file can be downloaded from a URL using WebClient, which is available in the System.Net namespace. The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI.
The WebClient class uses the WebRequest class internally to provide access to web resources. It offers a simple, high-level interface for downloading files without dealing with complex HTTP protocols directly.
Syntax
Following is the syntax for downloading a file using WebClient.DownloadFile method −
WebClient client = new WebClient();
client.DownloadFile("sourceUrl", "destinationPath");
Parameters
-
sourceUrl − The URL from which to download the file
-
destinationPath − The local path where the file will be saved
Using WebClient.DownloadFile
Example
using System;
using System.Net;
namespace DemoApplication {
public class Program {
public static void Main() {
string url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
string savePath = @"C:\temp\downloaded_file.pdf";
WebClient client = new WebClient();
try {
client.DownloadFile(url, savePath);
Console.WriteLine("File downloaded successfully to: " + savePath);
}
catch (Exception ex) {
Console.WriteLine("Error downloading file: " + ex.Message);
}
finally {
client.Dispose();
}
}
}
}
The output of the above code is −
File downloaded successfully to: C:\temp\downloaded_file.pdf
Using HttpClient (Modern Approach)
For modern .NET applications, HttpClient is the preferred approach as it's more flexible and supports async operations −
Example
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace DemoApplication {
public class Program {
public static async Task Main() {
string url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
string savePath = @"C:\temp\downloaded_with_httpclient.pdf";
using (HttpClient client = new HttpClient()) {
try {
byte[] fileBytes = await client.GetByteArrayAsync(url);
await File.WriteAllBytesAsync(savePath, fileBytes);
Console.WriteLine("File downloaded successfully using HttpClient");
Console.WriteLine("File size: " + fileBytes.Length + " bytes");
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
}
The output of the above code is −
File downloaded successfully using HttpClient File size: 13264 bytes
Comparison
| WebClient | HttpClient |
|---|---|
| Simple synchronous download | Supports async/await patterns |
| Legacy .NET Framework approach | Modern .NET approach |
| Less control over HTTP headers | Full control over HTTP requests |
| Implements IDisposable | Designed for reuse across requests |
Common Use Cases
-
Downloading images, documents, or media files from web servers
-
Saving web content for offline processing or backup
-
Retrieving configuration files or data files from remote locations
Conclusion
Both WebClient.DownloadFile and HttpClient provide effective ways to download files from URLs in C#. While WebClient offers simplicity for basic downloads, HttpClient is recommended for modern applications due to its async support and better resource management.
