My.Computer.Network.DownloadFile 方法

更新:2007 年 11 月

下载指定的远程文件,并将其保存在指定的位置。

' 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 _
)

参数

  • address
    String 或 Uri。要下载的文件的路径,其中包括文件名和主机地址。必选。

  • destinationFileName
    String。已下载文件的文件名和路径。必选。

  • userName
    String。要进行身份验证的用户名。默认值为空字符串,“”。

  • password
    String。要进行身份验证的密码。默认值为空字符串,“”。

  • showUI
    Boolean。指定是否显示操作进度。默认值为 False。

  • connectionTimeout
    Int32。超时间隔(以毫秒为单位)。默认值为 100 秒。

  • overwrite
    Boolean。指定是否覆盖现有文件。默认值为 False。

  • onUserCancel
    UICancelOption。指定当用户在对话框(在 ShowUI 设置为 True 时显示)上单击“取消”或“否”时的行为。默认值为 ThrowException

  • networkCredentials
    ICredentials。要提供的凭据。

异常

以下情况可能会导致异常:

备注

如果将 showUI 设置为 True,则会出现一个显示操作进度的对话框;该对话框包含可用于取消操作的“取消”按钮。由于该对话框不是模块对话框,因此不会妨碍用户在程序的其他窗口中输入内容。

如果服务器在指定的 connectionTimeout 内未响应,则会取消操作并引发异常。

如果在应用程序中启用了网络跟踪,DownloadFile 将输出跟踪信息。有关更多信息,请参见启用网络跟踪

说明:

DownloadFile 方法不发送可选 HTTP 标头。如果可选用户代理标头丢失,一些服务器会返回 500(内部服务器错误)。若要发送可选标头,必须使用 WebClient 类构造请求。有关更多信息,请参见 .NET Framework 中的网络操作 (Visual Basic)

安全说明:

FTP 协议会以纯文本方式发送信息(包括密码),因此不应用于传送敏感信息。

任务

下表列出了涉及 My.Computer.Network.DownloadFile 方法的任务的示例。

要执行的操作

请参见

下载文件

如何:在 Visual Basic 中下载文件

示例

此示例从 http://www.cohowinery.com/downloads 下载文件 WineList.txt,并将其保存到 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")

此示例从 http://www.cohowinery.com/downloads 下载文件 WineList.txt 并将其保存到 C:\Documents and Settings\All Users\Documents,同时指定一个 500 毫秒的超时间隔。

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

要求

命名空间:Microsoft.VisualBasic.Devices

类:Network

**程序集:**Visual Basic 运行库(位于 Microsoft.VisualBasic.dll 中)

按项目类型列出的可用性

项目类型

可用

Windows 应用程序

类库

控制台应用程序

Windows 控件库

Web 控件库

Windows 服务

网站

权限

以下权限可能是必需的:

权限

说明

FileIOPermission

控制访问文件和文件夹的能力。关联的枚举:Unrestricted

UIPermission

控制与用户界面和剪贴板相关的权限。关联的枚举:AllWindows

WebPermission

控制访问 HTTP Internet 资源的权限。关联的枚举:Unrestricted

有关更多信息,请参见代码访问安全性请求权限

请参见

任务

如何:在 Visual Basic 中下载文件

如何:在 Visual Basic 中确定远程计算机是否可用

如何:在 Visual Basic 中分析文件路径

参考

My.Computer.Network 对象

System.Uri

System.Net.ICredentials

Network.DownloadFile