How to: Download a File in Visual Basic
The DownloadFile method can be used to download a remote file and store it to a specific location. If the ShowUI
parameter is set to True
, a dialog box is displayed showing the progress of the download and allowing users to cancel the operation. By default, existing files having the same name are not overwritten; if you want to overwrite existing files, set the overwrite
parameter to True
.
The following conditions may cause an exception:
Drive name is not valid (ArgumentException).
Necessary authentication has not been supplied (UnauthorizedAccessException or SecurityException).
The server does not respond within the specified
connectionTimeout
(TimeoutException).The request is denied by the Web site (WebException).
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalizing the IDE.
Important
Do not make decisions about the contents of the file based on the name of the file. For example, the file Form1.vb may not be a Visual Basic source file. Verify all inputs before using the data in your application. The contents of the file may not be what is expected, and methods to read from the file may fail.
To download a file
Use the
DownloadFile
method to download the file, specifying the target file's location as a string or URI and specifying the location at which to store the file. This example downloads the fileWineList.txt
fromhttp://www.cohowinery.com/downloads
and saves it toC:\Documents and Settings\All Users\Documents
:My.Computer.Network.DownloadFile( "http://www.cohowinery.com/downloads/WineList.txt", "C:\Documents and Settings\All Users\Documents\WineList.txt")
To download a file, specifying a time-out interval
Use the
DownloadFile
method to download the file, specifying the target file's location as a string or URI, specifying the location at which to store the file, and specifying the time-out interval in milliseconds (the default is 1000). This example downloads the fileWineList.txt
fromhttp://www.cohowinery.com/downloads
and saves it toC:\Documents and Settings\All Users\Documents
, specifying a time-out interval of 500 milliseconds:My.Computer.Network.DownloadFile( "http://www.cohowinery.com/downloads/WineList.txt", "C:\Documents and Settings\All Users\Documents\WineList.txt", False, 500)
To download a file, supplying a user name and password
Use the
DownLoadFile
method to download the file, specifying the target file's location as a string or URI and specifying the location at which to store the file, the user name, and the password. This example downloads the fileWineList.txt
fromhttp://www.cohowinery.com/downloads
and saves it toC:\Documents and Settings\All Users\Documents
, with the user nameanonymous
and a blank password.My.Computer.Network.DownloadFile( "http://www.cohowinery.com/downloads/WineList.txt", "C:\Documents and Settings\All Users\Documents\WineList.txt", "anonymous", "")
Important
The FTP protocol used by the
DownLoadFile
method sends information, including passwords, in plain text and should not be used for transmitting sensitive information.