如何在vb.net中向更新程序添加DownloadFileAsync

如何在vb.net中向更新程序添加DownloadFileAsync,vb.net,asynchronous,Vb.net,Asynchronous,我正在制作一个自动文件下载程序,当我按下按钮时,我需要它来重新下载和覆盖文件,但我不希望它在下载时挂起。 我不知道如何实现DownloadFileAsync到这段代码。请帮忙 这是我的密码: Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Button1.Enabled = False Button1.Text = "Updating..."

我正在制作一个自动文件下载程序,当我按下按钮时,我需要它来重新下载和覆盖文件,但我不希望它在下载时挂起。 我不知道如何实现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

提前谢谢

您可以使用
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

您可以使用
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