Export (0) Print
Expand All

My.Computer.Network.DownloadFile Method 

Downloads the specified remote file and saves it in the specified location.


' Usage
My.Computer.Network.DownloadFile(address ,destinationFileName)
My.Computer.Network.DownloadFile(address ,destinationFileName)
My.Computer.Network.DownloadFile(address ,destinationFileName ,userName ,password)
My.Computer.Network.DownloadFile(address ,destinationFileName ,userName ,password)
My.Computer.Network.DownloadFile(address ,destinationFileName ,userName ,password ,showUI ,connectionTimeout ,overwrite)
My.Computer.Network.DownloadFile(address ,destinationFileName ,userName ,password ,showUI ,connectionTimeout ,overwrite ,onUserCancel)
My.Computer.Network.DownloadFile(address ,destinationFileName ,userName ,password ,showUI ,connectionTimeout ,overwrite)
My.Computer.Network.DownloadFile(address ,destinationFileName ,userName ,password ,showUI ,connectionTimeout ,overwrite ,onUserCancel)
My.Computer.Network.DownloadFile(address ,destinationFileName ,networkCredentials ,showUI ,connectionTimeout ,overwrite)
My.Computer.Network.DownloadFile(address ,destinationFileName ,networkCredentials ,showUI ,connectionTimeout ,overwrite ,onUserCancel)
' Declaration
Public Sub DownloadFile( _
   ByVal address As String, _
   ByVal destinationFileName As String _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As System.Uri, _
   ByVal destinationFileName As String _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As String, _
   ByVal destinationFileName As String, _
   ByVal userName As String, _
   ByVal password As String _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As System.Uri, _
   ByVal destinationFileName As String, _
   ByVal userName As String, _
   ByVal password As String _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As String, _
   ByVal destinationFileName As String, _
   ByVal userName As String, _
   ByVal password As String, _
   ByVal showUI As Boolean, _
   ByVal connectionTimeout As Integer, _
   ByVal overwrite As Boolean _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As String, _
   ByVal destinationFileName As String, _
   ByVal userName As String, _
   ByVal password As String, _
   ByVal showUI As Boolean, _
   ByVal connectionTimeout As Integer, _
   ByVal overwrite As Boolean, _
   ByVal onUserCancel As UICancelOption _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As System.Uri, _
   ByVal destinationFileName As String, _
   ByVal userName As String, _
   ByVal password As String, _
   ByVal showUI As Boolean, _
   ByVal connectionTimeout As Integer, _
   ByVal overwrite As Boolean _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As System.Uri, _
   ByVal destinationFileName As String, _
   ByVal userName As String, _
   ByVal password As String, _
   ByVal showUI As Boolean, _
   ByVal connectionTimeout As Integer, _
   ByVal overwrite As Boolean, _
   ByVal onUserCancel As UICancelOption _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As System.Uri, _
   ByVal destinationFileName As String, _
   ByVal networkCredentials As System.Net.ICredentials, _
   ByVal showUI As Boolean, _
   ByVal connectionTimeout As Integer, _
   ByVal overwrite As Boolean _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As System.Uri, _
   ByVal destinationFileName As String, _
   ByVal networkCredentials As System.Net.ICredentials, _
   ByVal showUI As Boolean, _
   ByVal connectionTimeout As Integer, _
   ByVal overwrite As Boolean, _
   ByVal onUserCancel As UICancelOption _
)

Parameters

address

String or Uri. Path of the file to download, including file name and host address. Required.

destinationFileName

String. File name and path of the downloaded file. Required.

userName

String. User name to authenticate. Default is an empty string, "".

password

String.Password to authenticate. Default is an empty string, "".

showUI

Boolean. Specifies whether to display the progress of the operation. Default is False.

connectionTimeout

Int32. Timeout interval, in milliseconds. Default is 100 seconds.

overwrite

Boolean. Specifies whether to overwrite existing files. Default is False.

onUserCancel

UICancelOption. Specifies behavior when the user clicks Cancel or No on the dialog box shown as a result of ShowUI set to True. Default is ThrowException.

networkCredentials

ICredentials. Credentials to be supplied.

The following conditions may cause an exception to be thrown:

  • The drive name is not valid (ArgumentException).

  • destinationFileName ends with a trailing slash (ArgumentException).

  • overwrite is set to False and the destination file already exists (IOException).

  • The server does not respond within the specified connectionTimeout (TimeoutException).

  • The authentication fails (SecurityException).

  • User lacks necessary permissions (SecurityException).

  • The request is denied by the website (WebException).

If showUI is set to True, a dialog box appears that shows the progress of the operation; the dialog box contains a Cancel button that can be used to cancel the operation. The dialog box is not modal, and therefore does not block user input to other windows in the program.

If the server does not respond within the specified connectionTimeout, the operation is cancelled, and an exception is thrown.

DownloadFile outputs trace information when you enable network tracing in your application. For more information, see Enabling Network Tracing.

NoteNote

The DownloadFile method does not send optional HTTP headers. Some servers may return 500 (Internal Server Error) if the optional user agent header is missing. To send optional headers, you must construct a request using the WebClient class. For more information, see Network Operations in the .NET Framework with Visual Basic.

Security noteSecurity Note

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

The following table lists an example of a task involving the My.Computer.Network.DownloadFile method.

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")

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 timeout interval of 500 milliseconds.

My.Computer.Network.DownloadFile _
("http://www.cohowinery.com/downloads/", _
"C:\Documents and Settings\All Users\Documents\WineList.txt", _
"", "", False, 500, True)

Namespace: Microsoft.VisualBasic.Devices

Class: Network

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

The following permissions may be necessary:

Permission Description

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

UIPermission

Controls the permissions related to user interfaces and the clipboard. Associated enumeration: AllWindows.

WebPermission

Controls rights to access HTTP Internet resources. Associated enumeration: Unrestricted.

For more information, see Code Access Security and Requesting Permissions.

Community Additions

ADD
Show:
© 2014 Microsoft