I decided to add some download options for my tool and people that use it started reporting that they get an error when the program tries to download the files and install them.
Here is the code I use:
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://ift.tt/1DQmKsW"), 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
My button simply does: DownloadFiles() and it goes from there... What error I get is, as if, the program cannot find the downloaded .zip file!
Another interesting factor is, the program doesn't find the downloaded .zip file on the first run! If you download those same files 2nd time it works fine...
So I came to a conclusion. The program downloads the .zip file and rushes to unzip it - which causes the confusion. The .zip isn't written all the way. But Web Client already reports it as downloaded.
I tried to use Threading.Thread.Sleep(3000) after the download is over. To make sure the file gets to the computer "all the way" but it didn't fix it :(
So I need some help! Since I'm a newbie with Web Client.
Aucun commentaire:
Enregistrer un commentaire