
Download files in C#
File Downloader Using C# Requirements Valid File URL/List of URLs IDE: Visual Studio or Visual Studio Code (preferred) Latest .NET SDK Installed: Download Here Steps 1. Open Visual Studio or Visual Studio Code 2. Paste the below snippet 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 using var httpClient = new HttpClient(); // Replace the URL with the actual File URL string fileUrl = "https://drive.usercontent.google.com/download?id=11PIu2A0ICZHh46phJMFZ3Y547gQDEI7T&export=download&authuser=0"; try { // Send GET request to fetch the File using HttpResponseMessage response = await httpClient.GetAsync(fileUrl); // Check if request was successful if (response.IsSuccessStatusCode) { // Get the File stream using (Stream fileStream = await response.Content.ReadAsStreamAsync()) { // Save the file with appropriate extension name var fileName=$"image.png" using FileStream fileStream = File.Create(fileName); await fileStream.CopyToAsync(fileStream); } Console.WriteLine("File downloaded successfully."); } else { Console.WriteLine($"Failed to download file. Status code: {response.StatusCode}"); } } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } 3. Customize the Code Replace fileUrl with the URL of the file you want to download. Replace fileName with the desired name and extension for the downloaded file. 4. Run the Program For List of String you may use 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 var ls = new List<string>() { "https://picsum.photos/200/300", "https://picsum.photos/200/300" }; foreach (var item in ls) { try { // Send GET request to fetch the File using HttpResponseMessage response = await httpClient.GetAsync(fileUrl); // Check if request was successful /* rest code */ } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } }