次の方法で共有


WebClient.DownloadFile メソッド

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

Public Sub DownloadFile( _
   ByVal address As String, _   ByVal fileName As String _)
[C#]
public void DownloadFile(stringaddress,stringfileName);
[C++]
public: void DownloadFile(String* address,String* fileName);
[JScript]
public function DownloadFile(
   address : String,fileName : String);

パラメータ

  • address
    データのダウンロード元の URI。
  • fileName
    データを受信するローカル ファイルの名前。

例外

例外の種類 条件
WebException BaseAddress および address を組み合わせて形成された URI が無効です。

または

filename が null 参照 (Visual Basic では Nothing) または Empty です。

または

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

SecurityException 呼び出し元に、ローカル ファイルに対する書き込み許可がありません。

解説

DownloadFile メソッドは、 address パラメータで指定した URI からローカル ファイルにデータをダウンロードします。

BaseAddress プロパティが空でない場合、 address は、相対 URI である必要があります。この相対 URI は、 BaseAddress と結合されて、要求されたデータの絶対 URI を形成します。 QueryString プロパティが空でない場合は、 address に追加されます。

使用例

[Visual Basic, C#, C++] https://www.contoso.com のファイルをローカル ハード ドライブにダウンロードする例を次に示します。

 
Dim remoteUri As String = "https://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))

[C#] 
string remoteUri = "https://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);

[C++] 
String* remoteUri = S"https://www.contoso.com/library/homepage/images/";
String* fileName = S"ms-banner.gif", * myStringWebResource = 0;
// Create a new WebClient instance.
WebClient* myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = String::Concat(remoteUri, fileName);
Console::WriteLine(S"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(S"Successfully Downloaded File \" {0}\" from \" {1}\"", fileName, myStringWebResource);
   Console::WriteLine(S"\nDownloaded file saved in the following file system folder:\n\t {0}", Application::StartupPath);

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard

参照

WebClient クラス | WebClient メンバ | System.Net 名前空間