ファイルのダウンロード


HTTPでのWebサーバ上のファイルをダウンロードするサンプル。
1は進行状況のダイアログがなし、2は進行状況のダイアログあり。
また、2についてはユーザとパスワードを指定できるがフォルダに権限を設定していなければ指定する必要はない。


サンプルソース

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim wkWebClt As New System.Net.WebClient()

        'ダウンロード先のアドレス
        Dim adress As String = "http://localhost/haradaLab/download.txt"
        '保存する場所を指定(ファイル名まで指定すること)
        Dim destPath As String = "C:\Documents and Settings\administrator\デスクトップ\はらだ用\download\download.txt"

        '1.同期的にファイルをダウンロード
        wkWebClt.DownloadFile(adress, destPath)
        wkWebClt.Dispose()

        '2.ダウンロードの進行状況を表示(.NET Framework 2.0以降)
        My.Computer.Network.DownloadFile( _
          adress, destPath, _
            "administrator", "pass", _
            True, 60000, True, FileIO.UICancelOption.DoNothing)

    End Sub

End Class


IISの構成

規定のWebサイト
 |
 |---haradaLab
       |
       |---download.txt


動作確認環境:VisualStadio2005,.NET Framework 1.1,2.0