WebClient.DownloadFile 方法

定义

将具有指定 URI 的资源下载到本地文件。

重载

DownloadFile(Uri, String)

将具有指定 URI 的资源下载到本地文件。

DownloadFile(String, String)

将具有指定 URI 的资源下载到本地文件。

DownloadFile(Uri, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

将具有指定 URI 的资源下载到本地文件。

public:
 void DownloadFile(Uri ^ address, System::String ^ fileName);
public void DownloadFile (Uri address, string fileName);
member this.DownloadFile : Uri * string -> unit
Public Sub DownloadFile (address As Uri, fileName As String)

参数

address
Uri

String 形式指定的 URI,将从中下载数据。

fileName
String

要接收数据的本地文件的名称。

例外

address 参数为 null

- 或 -

fileName 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

filenamenullEmpty

- 或 -

文件不存在。

- 或 -

下载数据时发生错误。

该方法已在多个线程上同时调用。

注解

方法 DownloadFile 从 参数中指定的 address URI 下载到本地文件数据。 此方法在下载资源时阻止。 若要下载资源并在等待服务器响应时继续执行,请使用 方法之 DownloadFileAsync 一。

BaseAddress如果属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

此方法使用 RETR 命令下载 FTP 资源。 对于 HTTP 资源,使用 GET 方法。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

在中间层应用程序(如 ASP.NET 页)中使用此方法时,如果执行应用程序的帐户无权访问文件,则会收到错误。

适用于

DownloadFile(String, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

将具有指定 URI 的资源下载到本地文件。

public:
 void DownloadFile(System::String ^ address, System::String ^ fileName);
public void DownloadFile (string address, string fileName);
member this.DownloadFile : string * string -> unit
Public Sub DownloadFile (address As String, fileName As String)

参数

address
String

从中下载数据的 URI。

fileName
String

要接收数据的本地文件的名称。

例外

address 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

filenamenullEmpty

- 或 -

文件不存在。

- 或 - 下载数据时发生错误。

该方法已在多个线程上同时调用。

示例

下面的代码示例将 文件从 http://www.contoso.com 下载到本地硬盘驱动器。

String^ remoteUri = "http://www.contoso.com/library/homepage/images/";
String^ fileName = "ms-banner.gif", ^myStringWebResource = nullptr;
// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
// Concatenate the domain with the Web resource filename.
myStringWebResource = String::Concat( 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 {0}", Application::StartupPath );
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);
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))

注解

方法 DownloadFile 从 参数中指定的 address URI 下载到本地文件数据。 此方法在下载资源时阻止。 若要下载资源并在等待服务器响应时继续执行,请使用 方法之 DownloadFileAsync 一。

BaseAddress如果属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

此方法使用 RETR 命令下载 FTP 资源。 对于 HTTP 资源,使用 GET 方法。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

在中间层应用程序(如 ASP.NET 页)中使用此方法时,如果执行应用程序的帐户无权访问文件,则会收到错误。

适用于