Download a file from a URL in C#
This post will discuss how to download a file from a URL in C#.
1. Using WebClient Class
The System.Net.WebClient class provides several utility methods for sending data and receiving data from a URI resource. You can use the WebClient.DownloadFile() method to download the resource with the specified URI to a local file. It takes two parameters – address, which is the URI from which to download data and the fileName, which is the name of the local file that is to receive the data. The following example downloads the jquery-3.6.1.min.js file from the https://code.jquery.com/ domain and saves the file to the local hard drive.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Net; public class Example { public static void Main() { string remoteUri = "https://code.jquery.com/"; // Target Domain string fileName = "jquery-3.6.1.min.js"; // Web resource filename string dest = @$"D:\{fileName}"; // Local file using (WebClient webClient = new WebClient()) { Console.WriteLine("Downloading file {0}...", fileName); // Download the Web resource and save it into the destination webClient.DownloadFile(remoteUri + fileName, dest); Console.WriteLine("Successfully downloaded file {0}", fileName); } } } |
Note that this method blocks the calling thread while downloading the resource. To download a resource asynchronously, consider using the DownloadFileAsync() method. You can also use the WebRequest class to make a request to a URI, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; using System.IO; using System.Net; public class Example { public static void Main() { String url = "https://code.jquery.com/jquery-3.6.1.min.js"; string dest = @"D:\jquery-3.6.1.min.js"; using (StreamReader reader = new StreamReader(WebRequest.Create(url) .GetResponse().GetResponseStream())) { String content = reader.ReadToEnd(); File.WriteAllText(dest, content); } } } |
Note that both the WebClient and WebRequest class are obsolete and results in the following warning.
warning SYSLIB0014: ‘WebClient.WebClient()’ is obsolete: ‘WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.’
2. Using HttpClient Class
It is preferable to use the HttpClient class from the System.Net.Http namespace. You can use the HttpClient.GetStringAsync() method to asynchronously return the response as a string from the specified Uri, which then can be written to the file system.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; using System.IO; using System.Net.Http; public class Example { public static void Main() { String url = "https://code.jquery.com/jquery-3.6.1.min.js"; string content; using (HttpClient client = new HttpClient()) { content = client.GetStringAsync(url).Result; } string dest = @"D:\jquery-3.6.1.min.js"; File.WriteAllText(dest, content); } } |
That’s all about downloading a file from a URL in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)