Vb.net 如何使用进度条下载.exe文件-VB 2012

Vb.net 如何使用进度条下载.exe文件-VB 2012,vb.net,download,Vb.net,Download,我正在尝试为我的程序创建一个更新程序,它会自动从网上下载我程序的最新版本。现在,我希望使用进度条完成这个过程(因此,当下载进度为50%时,进度条已完成一半)。这是我的代码: Private Sub client_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Dim bytesIn As Double = Double.Parse(e.BytesReceived.T

我正在尝试为我的程序创建一个更新程序,它会自动从网上下载我程序的最新版本。现在,我希望使用进度条完成这个过程(因此,当下载进度为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方法,我将无法使用进度条跟踪下载进度。非常感谢您的帮助:-)

不知道您的意思,当您说文件未下载时?你得到一个错误/异常?什么都没发生?是否放置断点、debug.prints等

使用VS2012和asyn/await,您可以将所有内容放在一个方法中,以保持“线性代码流”。导入System.Threading和System.Threading.Tasks

Private Async Function DownloadWithProgress(ByVal url As String, ByVal p As ProgressBar) As Task(Of Integer)

    Dim wc As Net.HttpWebRequest = DirectCast(Net.HttpWebRequest.Create(url), Net.HttpWebRequest)
    Dim resp = Await (New TaskFactory(Of Net.WebResponse)).StartNew(AddressOf wc.GetResponse)
    p.Value = 0
    p.Maximum = CInt(resp.ContentLength)
    Dim rqs = resp.GetResponseStream


    Dim bufsize As Integer = 1 << 16
    Dim buffer(bufsize) As Byte
    Dim got As Integer = 0
    Dim total As Integer = 0
    Do
        got = Await (New TaskFactory(Of Integer)).FromAsync(AddressOf rqs.BeginRead, AddressOf rqs.EndRead, buffer, 0, bufsize, Nothing)
        total += got
        Me.Label1.Text = "got: " & total.ToString
        p.Increment(got)
    Loop Until got = 0

    Return total

End Function

Hi的可能副本,谢谢你的评论,但是当我执行你的代码时,我得到以下错误:错误1类型“任务”未定义。C:\Users\User\AppData\Local\Temporary Projects\WindowsApplication1\Form1。vb 3 97 WindowsApplication1错误2类型“TaskFactory”未定义。C:\Users\User\AppData\Local\TemporaryProjects\WindowsApplication1\Form1.vb 6 31 WindowsApplication1错误3类型“TaskFactory”未定义。C:\Users\User\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 17 30 WindowsApplication1错误4“Wait”仅当包含在标记为“Async”的方法或lambda表达式中时才能使用modifier.C:\Users\Milad\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 39 9 WindowsApplication1您需要导入所需的名称空间;在帖子中添加了信息。谢谢伙计:()()()())())())(
Private running As Boolean = False
Private Async Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    If running Then
        MessageBox.Show("Cant you see, I'm working?")
        Exit Sub
    Else
        running = True
    End If

    Await DownloadWithProgress("http://download.thinkbroadband.com/5MB.zip", ProgressBar1)
    running = False

End Sub