WebClient.DownloadFile 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將具有指定之 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)
參數
- fileName
- String
要接收資料的本機檔案名稱。
例外狀況
已經在多個執行緒上同時呼叫此方法。
備註
方法 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
。
已經在多個執行緒上同時呼叫此方法。
範例
下列程式代碼範例會將檔案從 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 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應