我正在尝试为我的程序创建一个更新程序,它会自动从网上下载我的程序的最新版本。现在我希望使用进度条完成此过程(因此当下载进度为 50% 时,进度条为中途)。这是我的代码:
Private Sub client_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString())
Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString())
Dim percentage As Double = bytesIn / totalBytes * 100
client.Value = Int32.Parse(Math.Truncate(percentage).ToString())
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim url As String = "MY DOWNLOAD LINK"
'Download
Dim client As WebClient = New WebClient
AddHandler client.DownloadProgressChanged, AddressOf client_ProgressChanged
AddHandler client.DownloadFileCompleted, AddressOf client_DownloadCompleted
client.DownloadFileAsync(New Uri(url), "C:\Users\User\Desktop\BACKUP\TESTING\New folder\1.exe")
End Sub
End Class
现在我知道保存文件的位置是我手动输入的,但我稍后会更改。我目前的问题是文件没有被下载。但是,当我将 DownloadFileAsync 方法更改为 DownloadFile 时,我的程序会下载该文件。但是,使用 DownloadFile 方法,我将无法使用进度条来跟踪下载进度。任何帮助深表感谢 :-)