如何将 DownloadFileAsync 添加到 vb.net 中的更新程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15341377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add DownloadFileAsync to updater program in vb.net
提问by TheCreepySheep
I'm making a automatic file downloader and I need it to redownload and overwrite the file, when i press the button, but i don't want it to hang when it is downloading. I don't know how to implement DownloadFileAsync to this code. Please help!
我正在制作一个自动文件下载器,当我按下按钮时,我需要它来重新下载和覆盖文件,但我不希望它在下载时挂起。我不知道如何对这段代码实现 DownloadFileAsync。请帮忙!
Here is my code:
这是我的代码:
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
Thanks in advance!
提前致谢!
采纳答案by Parimal Raj
You can use the System.Net.WebClientto download a file in async mode.
您可以使用System.Net.WebClient以异步模式下载文件。
Sample Code :
示例代码:
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

