Share via


My.Computer.Network.DownloadFile メソッド

更新 : 2007 年 11 月

指定のリモート ファイルをダウンロードし、指定の場所に保存します。

' Usage
My.Computer.Network.DownloadFile(address ,destinationFileName)
My.Computer.Network.DownloadFile(address ,destinationFileName)
My.Computer.Network.DownloadFile(address ,destinationFileName ,userName ,password)
My.Computer.Network.DownloadFile(address ,destinationFileName ,userName ,password)
My.Computer.Network.DownloadFile(address ,destinationFileName ,userName ,password ,showUI ,connectionTimeout ,overwrite)
My.Computer.Network.DownloadFile(address ,destinationFileName ,userName ,password ,showUI ,connectionTimeout ,overwrite ,onUserCancel)
My.Computer.Network.DownloadFile(address ,destinationFileName ,userName ,password ,showUI ,connectionTimeout ,overwrite)
My.Computer.Network.DownloadFile(address ,destinationFileName ,userName ,password ,showUI ,connectionTimeout ,overwrite ,onUserCancel)
My.Computer.Network.DownloadFile(address ,destinationFileName ,networkCredentials ,showUI ,connectionTimeout ,overwrite)
My.Computer.Network.DownloadFile(address ,destinationFileName ,networkCredentials ,showUI ,connectionTimeout ,overwrite ,onUserCancel)
' Declaration
Public Sub DownloadFile( _
   ByVal address As String, _
   ByVal destinationFileName As String _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As System.Uri, _
   ByVal destinationFileName As String _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As String, _
   ByVal destinationFileName As String, _
   ByVal userName As String, _
   ByVal password As String _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As System.Uri, _
   ByVal destinationFileName As String, _
   ByVal userName As String, _
   ByVal password As String _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As String, _
   ByVal destinationFileName As String, _
   ByVal userName As String, _
   ByVal password As String, _
   ByVal showUI As Boolean, _
   ByVal connectionTimeout As Integer, _
   ByVal overwrite As Boolean _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As String, _
   ByVal destinationFileName As String, _
   ByVal userName As String, _
   ByVal password As String, _
   ByVal showUI As Boolean, _
   ByVal connectionTimeout As Integer, _
   ByVal overwrite As Boolean, _
   ByVal onUserCancel As UICancelOption _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As System.Uri, _
   ByVal destinationFileName As String, _
   ByVal userName As String, _
   ByVal password As String, _
   ByVal showUI As Boolean, _
   ByVal connectionTimeout As Integer, _
   ByVal overwrite As Boolean _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As System.Uri, _
   ByVal destinationFileName As String, _
   ByVal userName As String, _
   ByVal password As String, _
   ByVal showUI As Boolean, _
   ByVal connectionTimeout As Integer, _
   ByVal overwrite As Boolean, _
   ByVal onUserCancel As UICancelOption _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As System.Uri, _
   ByVal destinationFileName As String, _
   ByVal networkCredentials As System.Net.ICredentials, _
   ByVal showUI As Boolean, _
   ByVal connectionTimeout As Integer, _
   ByVal overwrite As Boolean _
)
' -or-
Public Sub DownloadFile( _
   ByVal address As System.Uri, _
   ByVal destinationFileName As String, _
   ByVal networkCredentials As System.Net.ICredentials, _
   ByVal showUI As Boolean, _
   ByVal connectionTimeout As Integer, _
   ByVal overwrite As Boolean, _
   ByVal onUserCancel As UICancelOption _
)

パラメータ

  • address
    String または Uri。ダウンロードするファイルのパスを、ファイル名とホスト アドレスを含めて指定します。必ず指定します。

  • destinationFileName
    String です。ダウンロードしたファイルの保存時のファイル名とパスを指定します。必ず指定します。

  • userName
    String です。認証するためのユーザー名を指定します。既定値は空の文字列 ("") です。

  • password
    String です。認証するためのパスワードを指定します。既定値は空の文字列 ("") です。

  • showUI
    Boolean です。操作の進行状況を表示するかどうかを指定します。既定値は False です。

  • connectionTimeout
    Int32 です。タイムアウト時間をミリ秒を単位として指定します。既定値は 100 秒です。

  • overwrite
    Boolean です。既存のファイルを上書きするかどうかを指定します。既定値は False です。

  • onUserCancel
    UICancelOption です。ShowUI を True に設定した結果として表示されるダイアログ ボックスで、ユーザーが [キャンセル] または [いいえ] をクリックしたときに、どのような動作を実行するかを指定します。既定値は ThrowException です。

  • networkCredentials
    ICredentials です。資格情報を指定します。

例外

例外がスローされる可能性のある状況を次に示します。

  • ドライブ名が無効です (ArgumentException)。

  • destinationFileName がスラッシュで終わっています (ArgumentException)。

  • overwrite が False に設定されていますが、コピー先のファイルが既に存在します (IOException)。

  • サーバーが、指定された connectionTimeout の間に応答しません (TimeoutException)。

  • 認証がエラーになります (SecurityException)。

  • ユーザーに必要なアクセス許可がありません (SecurityException)。

  • 要求が Web サイトによって拒否されます (WebException)。

解説

showUI が True に設定されている場合、操作の進行状況を示すダイアログ ボックスが表示されます。このダイアログ ボックスにある [キャンセル] ボタンをクリックすると、操作をキャンセルできます。このダイアログ ボックスはモーダルではないため、プログラムの他のウィンドウへの入力を妨げません。

サーバーが指定の connectionTimeout の間に応答しない場合、操作はキャンセルされ、例外がスローされます。

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

メモ :

DownloadFile メソッドは、オプションの HTTP ヘッダーを送信しません。サーバーによっては、オプションのユーザー エージェント ヘッダーが見つからない場合に 500 (内部サーバー エラー) を返すこともあります。オプションのヘッダーを送信するには、WebClient クラスを使用して要求を作成する必要があります。詳細については、「Visual Basic による .NET Framework でのネットワーク操作」を参照してください。

セキュリティに関するメモ :

FTP プロトコルは、パスワードを含む情報をプレーンテキストで送信するため、重要な情報の送信には使用しないでください。

処理手順

My.Computer.Network.DownloadFile メソッドに関連するタスクの例を次の表に示します。

タスク

参照項目

ファイルをダウンロードします。

方法 : Visual Basic でファイルをダウンロードする

使用例

次のコード例は、WineList.txt というファイルを http://www.cohowinery.com/downloads からダウンロードし、C:\Documents and Settings\All Users\Documents に保存します。

My.Computer.Network.DownloadFile _
("http://www.cohowinery.com/downloads/WineList.txt", _
"C:\Documents and Settings\All Users\Documents\WineList.txt")

次のコード例は、タイアウト時間を 500 ミリ秒に設定して、WineList.txt というファイルを http://www.cohowinery.com/downloads からダウンロードし、C:\Documents and Settings\All Users\Documents に保存します。

My.Computer.Network.DownloadFile _
("http://www.cohowinery.com/downloads/", _
"C:\Documents and Settings\All Users\Documents\WineList.txt", _
"", "", False, 500, True)

必要条件

名前空間 : Microsoft.VisualBasic.Devices

クラス : Network

アセンブリ : Visual Basic ランタイム ライブラリ (Microsoft.VisualBasic.dll 内)

プロジェクトの種類別の可用性

プロジェクトの種類

使用可/不可

Windows アプリケーション

あり

クラス ライブラリ

あり

コンソール アプリケーション

あり

Windows コントロール ライブラリ

あり

Web コントロール ライブラリ

あり

Windows サービス

あり

Web サイト

あり

アクセス許可

以下のアクセス許可が必要な場合があります。

アクセス許可

説明

FileIOPermission

ファイルとフォルダへのアクセス許可を制御します。関連する列挙値 : Unrestricted

UIPermission

ユーザー インターフェイスおよびクリップボードに関連するアクセス許可を制御します。関連する列挙値 : AllWindows

WebPermission

HTTP インターネット リソースへのアクセス許可を制御します。関連する列挙値 : Unrestricted

詳細については、「コード アクセス セキュリティ」および「アクセス許可の要求」を参照してください。

参照

処理手順

方法 : Visual Basic でファイルをダウンロードする

方法 : Visual Basic でリモート コンピュータが利用可能かどうかを確認する

方法 : Visual Basic でファイル パスを解析する

参照

My.Computer.Network オブジェクト

System.Uri

System.Net.ICredentials

Network.DownloadFile