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
要接收数据的本地文件的名称。
例外
通过组合 BaseAddress 和 address
所构成的 URI 无效。
- 或 -
filename
是 null
或 Empty。
- 或 -
文件不存在。
- 或 -
下载数据时发生错误。
该方法已在多个线程上同时调用。
注解
方法 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
。
通过组合 BaseAddress 和 address
所构成的 URI 无效。
- 或 -
filename
是 null
或 Empty。
- 或 -
文件不存在。
- 或 - 下载数据时发生错误。
该方法已在多个线程上同时调用。
示例
下面的代码示例将 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
则必须是与 BaseAddress 组合在一起以构成所请求数据的绝对 URI 的相对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address
。
此方法使用 RETR 命令下载 FTP 资源。 对于 HTTP 资源,使用 GET 方法。
备注
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪。
在中间层应用程序(如 ASP.NET 页)中使用此方法时,如果执行应用程序的帐户没有访问文件的权限,则会收到错误。
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈