How to download a file from a URL in C#?
To download a file in C# we can useWebClient
object from System.Net
namespace library, this object has a built in method DownloadFile()
, the method takes 2 parameter, first parameter will be the remote URL of the file, second parameter will be the local path.Remember to include:
using System.Net;
using System;
using System.Net;
public class Example
{
public static void Main()
{
string url = "https://cdn.pixabay.com/photo/2013/10/02/23/03/mountains-190055_960_720.jpg";
string savePath = @"C:\FreeImages.jpg";
WebClient client = new WebClient();
client.DownloadFile(url, savePath);
}
}