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 が無効です。

-又は-

filenamenull または Emptyです。

-又は-

ファイルが存在しません。

-又は-

データのダウンロード中にエラーが発生しました。

このメソッドは、複数のスレッドで同時に呼び出されています。

注釈

注意

WebRequestHttpWebRequestServicePointWebClient は廃止されており、新しい開発には使用しないでください。 代わりに HttpClient を使用してください。

DownloadFile メソッドは、address パラメーターで指定された URI からローカル ファイル データにダウンロードします。 このメソッドは、リソースのダウンロード中にブロックします。 リソースをダウンロードし、サーバーの応答を待機しながら実行を続けるには、DownloadFileAsync のいずれかのメソッドを使用します。

BaseAddress プロパティが空の文字列 ("") ではなく、address に絶対 URI が含まれていない場合、address は、要求されたデータの絶対 URI を形成するために BaseAddress と組み合わせた相対 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 が無効です。

-又は-

filenamenull または 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))

注釈

注意

WebRequestHttpWebRequestServicePointWebClient は廃止されており、新しい開発には使用しないでください。 代わりに HttpClient を使用してください。

DownloadFile メソッドは、address パラメーターで指定された URI からローカル ファイル データにダウンロードします。 このメソッドは、リソースのダウンロード中にブロックします。 リソースをダウンロードし、サーバーの応答を待機しながら実行を続けるには、DownloadFileAsync のいずれかのメソッドを使用します。

BaseAddress プロパティが空の文字列 ("") ではなく、address に絶対 URI が含まれていない場合、address は、要求されたデータの絶対 URI を形成するために BaseAddress と組み合わせた相対 URI である必要があります。 QueryString プロパティが空の文字列でない場合は、addressに追加されます。

この方法では、RETR コマンドを使用して FTP リソースをダウンロードします。 HTTP リソースの場合、GET メソッドが使用されます。

手記

このメンバーは、アプリケーションでネットワーク トレースを有効にすると、トレース情報を出力します。 詳細については、「.NET Frameworkでのネットワーク トレースの 」を参照してください。

ASP.NET ページなど、中間層アプリケーションでこのメソッドを使用する場合、アプリケーションが実行されるアカウントにファイルへのアクセス許可がない場合、エラーが表示されます。

適用対象