1

自動ファイルダウンローダーを作成しています。ボタンを押したときにファイルを再ダウンロードして上書きする必要がありますが、ダウンロード中にハングしたくありません。このコードにDownloadFileAsyncを実装する方法がわかりません。助けてください!

これが私のコードです:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Button1.Enabled = False
        Button1.Text = "Updating..."
        WebBrowser1.Visible = True
        My.Computer.Network.DownloadFile _
          (address:="http://199.91.154.170/e9f6poiwfocg/pei02c8727sa720/Ultra+v08.zip", _
          destinationFileName:=Path.Combine(Environment.GetFolderPath( _
          Environment.SpecialFolder.ApplicationData), _
          "test/Test.zip"), _
          userName:=String.Empty, password:=String.Empty, _
          showUI:=False, connectionTimeout:=100000, _
          overwrite:=True)
        WebBrowser1.Visible = False
        Button1.Text = "Updated!"
        PictureBox1.Visible = True

    End Sub

前もって感謝します!

4

1 に答える 1

1

を使用しSystem.Net.WebClientて、非同期モードでファイルをダウンロードできます。

サンプルコード:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim client As New WebClient()
    AddHandler client.DownloadProgressChanged, AddressOf ShowDownloadProgress
    AddHandler client.DownloadFileCompleted, AddressOf OnDownloadComplete
    client.DownloadFileAsync(New Uri("http://199.91.154.170/e9f6poiwfocg/pei02c8727sa720/Ultra v08.zip"), "test/Test.zip")


End Sub

Private Sub OnDownloadComplete(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
    If Not e.Cancelled AndAlso e.Error Is Nothing Then
        MessageBox.Show("DOwnload success")
    Else
        MessageBox.Show("Download failed")
    End If
End Sub

Private Sub ShowDownloadProgress(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
    ProgressBar1.Value = e.ProgressPercentage
End Sub
于 2013-03-11T14:55:15.493 に答える