WebClient.DownloadFile 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将具有指定 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)
参数
- fileName
- String
要接收数据的本地文件的名称。
例外
该方法已在多个线程上同时调用。
注解
谨慎
WebRequest、HttpWebRequest、ServicePoint和 WebClient 已过时,不应将其用于新开发。 请改用 HttpClient。
DownloadFile 方法从 address 参数中指定的 URI 下载到本地文件数据。 此方法在下载资源时会阻止。 若要下载资源并在等待服务器的响应时继续执行,请使用 DownloadFileAsync 方法之一。
如果 BaseAddress 属性不是空字符串(“”)且 address 不包含绝对 URI,address 必须是与 BaseAddress 相结合的相对 URI,才能形成所请求数据的绝对 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。
该方法已在多个线程上同时调用。
示例
下面的代码示例将文件从 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))
注解
谨慎
WebRequest、HttpWebRequest、ServicePoint和 WebClient 已过时,不应将其用于新开发。 请改用 HttpClient。
DownloadFile 方法从 address 参数中指定的 URI 下载到本地文件数据。 此方法在下载资源时会阻止。 若要下载资源并在等待服务器的响应时继续执行,请使用 DownloadFileAsync 方法之一。
如果 BaseAddress 属性不是空字符串(“”)且 address 不包含绝对 URI,address 必须是与 BaseAddress 相结合的相对 URI,才能形成所请求数据的绝对 URI。 如果 QueryString 属性不是空字符串,则会将其追加到 address。
此方法使用 RETR 命令下载 FTP 资源。 对于 HTTP 资源,将使用 GET 方法。
注意
在应用程序中启用网络跟踪时,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework中的
在中间层应用程序中(如 ASP.NET 页)中使用此方法时,如果执行应用程序的帐户无权访问文件,则会收到错误。