Bagikan melalui


How to: Download a File in Visual Basic

The My.Computer.Network.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:

Note

  The options available in dialog boxes, and the names and locations of menu commands you see, might differ from what is described in Help, depending on your active settings or edition. This Help page was written with General Development Settings in mind. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

Security noteSecurity Note:

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 file WineList.txt from http://www.cohowinery.com/downloads and saves it to C:\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 file WineList.txt from http://www.cohowinery.com/downloads and saves it to C:\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 file WineList.txt from http://www.cohowinery.com/downloads and saves it to C:\Documents and Settings\All Users\Documents, with the user name anonymous 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", "")
    
    Security noteSecurity Note:

    The FTP protocol used by the DownLoadFile method sends information, including passwords, in plain text and should not be used for transmitting sensitive information.

See Also

Tasks

How to: Upload a File in Visual Basic

How to: Parse File Paths in Visual Basic

Reference

My.Computer.Network Object

My.Computer.Network Object Members

My.Computer.Network.DownloadFile Method