Visual Basic中的Web客户端问题

时间:2015-04-14 01:26:20

标签: .net vb.net visual-studio runtime-error webclient

我决定为我的工具添加一些下载选项,使用它的人开始报告当程序尝试下载文件并安装它们时会出错。

以下是我使用的代码:

    Private Sub DownloadFiles()
    Dim Locales As WebClient = New WebClient
    AddHandler Locales.DownloadProgressChanged, AddressOf Locales_ProgressChanged
    AddHandler Locales.DownloadFileCompleted, AddressOf Locales_DownloadCompleted
    Label2.Text = "Status: Downloading files..."
    Locales.DownloadFileAsync(New Uri("http://updatesz.ucoz.com/ChangeServer.zip"), DirectoryToInstall & "\ChangeServer.zip")
End Sub

Private Sub Locales_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
    NsProgressBar1.Value = e.ProgressPercentage
    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
    NsProgressBar1.Value = Int32.Parse(Math.Truncate(percentage).ToString())
End Sub


Private Sub Locales_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    Label2.Text = "Status: Preparing..."
    NsProgressBar1.Value = 0
    DirectoryToInstall = GameDir.Text
    BackgroundWorker.WorkerReportsProgress = True
    Dim A As String() = {DirectoryToInstall, GameDir.Text}
    BackgroundWorker.RunWorkerAsync(A)
End Sub

我的按钮只是:DownloadFiles()它从那里开始......我得到的错误就是,程序无法找到下载的.zip文件!

另一个有趣的因素是,程序在第一次运行时找不到下载的.zip文件!如果你第二次下载这些相同的文件它工作正常......

所以我得出了一个结论。该程序下载.zip文件并急于解压缩 - 这会引起混淆。 .zip并不是一直写的。但Web客户端已将其报告为已下载。

下载结束后我尝试使用Threading.Thread.Sleep(3000)。确保文件一直到达计算机"#34;但它并没有解决它:(

所以我需要一些帮助!

我现在会搞乱代码!如果我发现任何有趣的东西(也许某种类型的修复,我将在下面发布)

事先谢谢你:)!

1 个答案:

答案 0 :(得分:0)

我将代码切换到:

My.Computer.Network.DownloadFile(("http://updatesz.ucoz.com/ChangeServer.zip"), DirectoryToInstall & "\ChangeServer.zip")

...并将代码添加到我已经内置的其他进度条中!虽然这不是我的目标,但在我完成之后我非常喜欢它。

相关问题