Introduction
The snippet shows how to copy image from web URL to local machine using WebClient.DownloadFile method with C# and VB.NET examples.
Sample Image:
We will use below image to copy from web URL to local machine.
Image URL : http://dotnetmirror.com/Resources/Uploaded-Files/cd690a7d-1d60-4a85-8c14-183810d191fe/sample copy imaage.png
C# Example
using System;
using System.Net;
namespace DotNetMirror
{
class SaveImageFromRemoteURL
{
public static void Main()
{
string remoteImageURLAddress = "http://dotnetmirror.com/Resources/Uploaded-Files/cd690a7d-1d60-4a85-8c14-183810d191fe/sample copy imaage.png";
string localFileName = @"sample copy imaage.png";
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile(remoteImageURLAddress, localFileName);
}
Console.WriteLine("copied image from remote location");
Console.ReadLine();
}
}
}
VB.NET Example
Imports System.Net
Namespace DotNetMirror
Class SaveImageFromRemoteURL
Public Shared Sub Main()
Dim remoteImageURLAddress As String = "http://dotnetmirror.com/Resources/Uploaded-Files/cd690a7d-1d60-4a85-8c14-183810d191fe/sample copy imaage.png"
Dim localFileName As String = "c:\sample copy imaage.png"
Using webClient As New WebClient()
webClient.DownloadFile(remoteImageURLAddress, localFileName)
End Using
Console.WriteLine("copied image from remote location")
Console.ReadLine()
End Sub
End Class
End Namespace
Output
Copies image from "http://dotnetmirror.com/Resources/Uploaded-Files/cd690a7d-1d60-4a85-8c14-183810d191fe/sample copy imaage.png" to local drive.
prints on console "copied image from remote location"
Explanation
WebClient has method DownloadFile which downloads the resource with the specified URI to a local file. We have to dispose webclient because webClient is implemented IDisposable interface.
Note: we have used example with image. You can specify any resource from web URL like .txt.