Downloads data from a resource with the specified URI to a local file.
[Visual Basic]
Public Sub DownloadFile( _
ByVal address As String, _
ByVal fileName As String _
)
[C#]
public void DownloadFile(
string address,
string fileName
);
[C++]
public: void DownloadFile(
String* address,
String* fileName
);
[JScript]
public function DownloadFile(
address : String,
fileName : String
);
Parameters
- address
- The URI to download data from.
- fileName
- The name of the local file to receive the data.
Exceptions
| Exception Type | Condition |
| WebException | The URI formed by combining BaseAddress and address is invalid.
-or- filename is a null reference (Nothing in Visual Basic), or Empty. -or- An error occurred while downloading data. |
| SecurityException | The caller does not have permission to write to local file. |
Remarks
The DownloadFile method downloads data from the URI specified by in the address parameter to a local file.
If the BaseAddress property is not empty, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. If the QueryString property is not empty, it is appended to address.
Example
[Visual Basic, C#, C++] The following example downloads a file from http://www.contoso.com to the local hard drive.
[Visual Basic]
Dim remoteUri As String = "http://www.contoso.com/library/homepage/images/"
Dim fileName As String = "ms-banner.gif"
Dim myStringWebResource As String = Nothing
' Create a new WebClient instance.
Dim myWebClient As New WebClient()
' Concatenate the domain with the Web resource filename. Because DownloadFile
'requires a fully qualified resource name, concatenate the domain with the Web resource file name.
myStringWebResource = remoteUri + fileName
Console.WriteLine("Downloading File ""{0}"" from ""{1}"" ......." + ControlChars.Cr + ControlChars.Cr, fileName, myStringWebResource)
' The DownloadFile() method downloads the Web resource and saves it into the current file-system folder.
myWebClient.DownloadFile(myStringWebResource, fileName)
Console.WriteLine("Successfully Downloaded file ""{0}"" from ""{1}""", fileName, myStringWebResource)
Console.WriteLine((ControlChars.Cr + "Downloaded file saved in the following file system folder:" + ControlChars.Cr + ControlChars.Tab + Application.StartupPath))
[C#]
string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
[C++]
String* remoteUri = S"http://www.contoso.com/library/homepage/images/";
String* fileName = S"ms-banner.gif", * myStringWebResource = 0;
// Create a new WebClient instance.
WebClient* myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = String::Concat(remoteUri, fileName);
Console::WriteLine(S"Downloading File \" {0}\" from \" {1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient->DownloadFile(myStringWebResource, fileName);
Console::WriteLine(S"Successfully Downloaded File \" {0}\" from \" {1}\"", fileName, myStringWebResource);
Console::WriteLine(S"\nDownloaded file saved in the following file system folder:\n\t {0}", Application::StartupPath);
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard
See Also
WebClient Class | WebClient Members | System.Net Namespace