WebClient.DownloadFile 方法

定義

將具有指定之 URI 的資源下載指本機檔案。

多載

DownloadFile(Uri, String)

將具有指定之 URI 的資源下載指本機檔案。

DownloadFile(String, String)

將具有指定之 URI 的資源下載指本機檔案。

DownloadFile(Uri, String)

來源:
WebClient.cs
來源:
WebClient.cs
來源:
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 則必須是結合 BaseAddress 的相對 URI,才能形成所要求資料的絕對 URI。 QueryString如果屬性不是空字串,則會附加至 address

此方法會使用 RETR 命令來下載 FTP 資源。 針對 HTTP 資源,會使用 GET 方法。

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework 中的網路追蹤

在仲介層應用程式中使用此方法時,例如 ASP.NET 網頁,如果應用程式執行所在的帳戶沒有存取檔案的許可權,您會收到錯誤。

適用於

DownloadFile(String, String)

來源:
WebClient.cs
來源:
WebClient.cs
來源:
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 則必須是結合 BaseAddress 的相對 URI,才能形成所要求資料的絕對 URI。 QueryString如果屬性不是空字串,則會附加至 address

此方法會使用 RETR 命令來下載 FTP 資源。 針對 HTTP 資源,會使用 GET 方法。

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework 中的網路追蹤

在仲介層應用程式中使用此方法時,例如 ASP.NET 網頁,如果應用程式執行所在的帳戶沒有存取檔案的許可權,您會收到錯誤。

適用於