ファイルダウンロードを行うには、System.NetネームスペースのWebClientクラスを利用すれば簡単に行うこと事ができます。
My.Computer.NetworkネームスペースのDownloadFileと同じだが、こちらは同期で実行されますが、
System.NetネームスペースのWebClientクラスには非同期で行えるメソッドなどが提供されています。
画面は、以下のようなフォームになります。
ダウンロード進捗状況変更時に発生するDownloadProgressChangedイベント・ダウンロードが完了した場合に発生するDownloadFileCompletedイベントを受け取れるように、以下を定義しておきます。
Private WithEvents _WebClient As New System.Net.WebClient
btnDownload_Clickイベントプロシージャは、以下のようになっています。
'--------------------------------------------------------------------------------
'「ダウンロード開始」ボタンクリック
'--------------------------------------------------------------------------------
Private Sub btnDownload_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDownload.Click
''入力項目チェック
If (txtDownloadUrl.Text.Length = 0) Then
MessageBox.Show("ダウンロードURLを入力してください。", "エラー", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
txtDownloadUrl.Focus()
Exit Sub
End If
If (txtSaveFilePath.Text.Length = 0) Then
MessageBox.Show("保存先を入力してください。", "エラー", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
txtSaveFilePath.Focus()
Exit Sub
End If
''ファイル有無チェック
If (My.Computer.FileSystem.FileExists(txtSaveFilePath.Text) = True) Then
If MessageBox.Show("ファイルが既に存在します。上書きしますか?", "確認", _
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = _
Windows.Forms.DialogResult.Cancel Then
Exit Sub
End If
End If
''ダウンロード開始
Dim uni As New Uri(txtDownloadUrl.Text)
Try
_WebClient.DownloadFileAsync(uni, txtSaveFilePath.Text)
txtDownloadUrl.ReadOnly = True
txtSaveFilePath.ReadOnly = True
btnDownload.Enabled = False
btnCancel.Enabled = True
Catch ex As Exception
MessageBox.Show(ex.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
DownloadFileAsyncメソッドが非同期で行う為のメソッドです。進捗状況DownloadProgressChangedイベント・処理が完了した場合には、DownloadFileCompletedイベントが発生します。
進捗状況DownloadProgressChangedイベントでは、プログレスバー・ダウンロードバイト数を表示するようにします。
'--------------------------------------------------------------------------------
'ダウンロード進捗状況変更イベント
'--------------------------------------------------------------------------------
Private Sub _WebClient_DownloadProgressChanged(ByVal sender As Object, _
ByVal e As System.Net.DownloadProgressChangedEventArgs) _
Handles _WebClient.DownloadProgressChanged
pgbarDownload.Value = e.ProgressPercentage
lblGetBytes.Text = e.BytesReceived.ToString & " / " & e.TotalBytesToReceive.ToString
End Sub
e.ProgressPercentageは現在ダウンロードの進捗状況のパーセント値(0から100)になっています。
処理が完了した場合に行う処理は、以下のようになっています。
'--------------------------------------------------------------------------------
'ダウンロード完了イベント
'--------------------------------------------------------------------------------
Private Sub _WebClient_DownloadFileCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.AsyncCompletedEventArgs) _
Handles _WebClient.DownloadFileCompleted
If (e.Cancelled = True) Then
lblGetBytes.Text = "ダウンロードは中止されました"
ElseIf (e.Error Is Nothing) Then
lblGetBytes.Text = "ダウンロードが完了しました"
Else
lblGetBytes.Text = e.Error.Message
End If
pgbarDownload.Value = 0
txtDownloadUrl.ReadOnly = False
txtSaveFilePath.ReadOnly = False
btnDownload.Enabled = True
btnCancel.Enabled = False
End Sub
また、処理を途中で中止したい場合は、CancelAsyncメソッドを呼び出すことで行えます。
サンプルを提供しますので、そちらで色々と試してみてください。