WebClient.DownloadFile メソッド

定義

指定した URI を持つリソースをローカル ファイルにダウンロードします。

オーバーロード

DownloadFile(Uri, String)

指定した URI を持つリソースをローカル ファイルにダウンロードします。

DownloadFile(String, String)

指定した URI を持つリソースをローカル ファイルにダウンロードします。

DownloadFile(Uri, String)

指定した 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 です。

BaseAddress および address を組み合わせて形成された URI が無効です。

  • または -

filenamenull または Empty です。

  • または -

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

  • または -

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

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

注釈

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

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

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

注意

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

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

適用対象

DownloadFile(String, String)

指定した 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 が無効です。

  • または -

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))

注釈

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

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

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

注意

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

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

適用対象