vb.net一次下载多个文件,错误?

时间:2014-11-29 18:32:14

标签: vb.net file download

我的程序使用了很多文件,如果有任何文件缺失,我会创建一个多文件下载程序,但我的下载程序也能正常工作,下载所有文件,然后应用程序崩溃。我的下载代码:

   Private Sub Download()
        If Main.FilesURL.Count = 0 = True Then
            MsgBox("No source file, the downloader will not start", vbCritical, "My program Download System")
        Else
            If Directory.Exists(Main.filePaths.Item(0)) = False Then
                Directory.CreateDirectory(Main.filePaths.Item(0))
            End If
            Client.DownloadFileAsync(New Uri(Main.FilesURL.Item(0)), Main.files.Item(0))
            AddHandler Client.DownloadProgressChanged, AddressOf Download_ProgressChanged
            AddHandler Client.DownloadFileCompleted, AddressOf Download_Completted
        End If
    End Sub

    Private Sub Download_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
        ProgressBar1.Value = Int32.Parse(Math.Truncate(percentage).ToString())
    End Sub

    Private Sub Download_Completted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
        RemoveHandler Client.DownloadProgressChanged, AddressOf Download_ProgressChanged
        RemoveHandler Client.DownloadFileCompleted, AddressOf Download_Completted
        If Main.files.Count = 0 = True Then
            Me.Hide()
            MsgBox("Game Updated!", vbInformation, "My Program")
            Main.Focus()
            Main.Button1.Text = "Play"
            Main.Button1.Enabled = True
            Main.Button2.Enabled = True
            Main.Button3.Enabled = True
            Main.Button4.Enabled = True
        Else
            Main.files.RemoveAt(0)
            Main.filePaths.RemoveAt(0)
            Main.FilesURL.RemoveAt(0)
            Download()
        End If
    End Sub

我使用字符串列表下载文件,添加文件,我创建了:

 If Not Directory.Exists(Root + "\versions\" + SelectedGameVersion + "\" + SelectedGameVersion + "-natives") Then
                Directory.CreateDirectory(Root + "\versions\" + SelectedGameVersion + "\" + SelectedGameVersion + "-natives")
            End If
            '//Loading Json:\\'
            Await jsonparser.loadjson()

            '//Loading Settings:\\'
            '//Converting Settings:\\'
            Dim i As Integer
            For i = 0 To Libraries.Count - 1
                files.Add(Root + "\libraries\" + stringconverter.ConvertToPath(Libraries.Item(i), Nothing, 0))
            Next
            '//Checking Files:\\'
            Dim i1 As Integer
            Dim i2 As Integer
            For i1 = 0 To files.Count - 1
                If File.Exists(files.Item(i1)) = False Then
                    For i2 = 0 To Libraries.Count - 1
                        FilesURL.Add(stringconverter.ConvertToURLPath(Libraries.Item(i2), Nothing, 0))
                    Next
                    Button1.Text = "Letöltés..."

                    If Not File.Exists(Root + "\versions\" + SelectedGameVersion + "\" + SelectedGameVersion + ".jar") Then
                        files.Add(Root + "\versions\" + SelectedGameVersion + "\" + SelectedGameVersion + ".jar")
                        FilesURL.Add("http://s3.amazonaws.com/Minecraft.Download/versions/" + SelectedGameVersion + "/" + SelectedGameVersion + ".jar")
                        filePaths.Add(Root + "\versions\" + SelectedGameVersion)
                    End If
                Else
                    Button1.Text = "Indítás..."
                    Launch = "OK"
                End If
            Next

            MsgBox(FilesURL.Count)
            If Not FilesURL.Count = 0 Then
                Downloader.ShowDialog()
            End If

        End If

但这不行,因为有33个文件,但在下载url(字符串列表)1233files。 有人可以告诉我错误吗?谢谢!

1 个答案:

答案 0 :(得分:0)

变化:

If Main.files.Count = 0 = True

要:

If Main.files.Count = 0

并改变:

If Main.FilesURL.Count = 0 = True

要:

If Main.FilesURL.Count = 0.