Backgroundworker没有开始复制

时间:2016-04-26 20:26:20

标签: vb.net backgroundworker

我是VB.NET的新手,我正在开发一个应用程序,读取文本文件的内容,并使用其中的路径进行文件/文件夹复制。我正在通过sum(self.foos)运行副本,而backgroundworker似乎没有line字符串。要进行故障排除,我已在行读取逻辑下放置MessageBox.Show(line)以查看它是否正在读取路径。它不是直接跳到我的BackgroundWorker1_RunWorkerCompleted子。

谁能看到我哪里错了?!

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As ComponentModel.DoWorkEventArgs, Optional ByVal Overwrite As Boolean = True) Handles BackgroundWorker1.DoWork

    Dim deststring As String

    ' Open config.txt with the Using statement.
    Using r As StreamReader = New StreamReader(Application.StartupPath + "\CONFIG.txt")

        Dim line As String

        ' Read first line.
        line = r.ReadLine

        ' Loop over each line in file, While list is Not Nothing.
        Do While (Not line Is Nothing)

            ' Read line and filter based on logic
            If line.StartsWith("*") Then
                r.ReadLine()
            ElseIf line.Contains(Destpath) Then
                r.ReadLine()
            ElseIf line.Equals("%USERPROFILE%\Desktop") Then
                line = desktoppath
            ElseIf line.Equals("%USERPROFILE%\Downloads") Then
                line = downloadsPath
            ElseIf line.Equals("%USERPROFILE%\Contacts") Then
                line = contactspath
            ElseIf line.Equals("%USERPROFILE%\Documents") Then
                line = documentspath
            ElseIf line.Equals("%USERPROFILE%\Favorites") Then
                line = favoritespath
            ElseIf line.Equals("%USERPROFILE%\Links") Then
                line = linkspath
            ElseIf line.Equals("%USERPROFILE%\Music") Then
                line = Musicpath
            ElseIf line.Equals("%USERPROFILE%\Pictures") Then
                line = picturespath
            ElseIf line.Equals("%USERPROFILE%\SavedGames") Then
                line = savedgamespath
            ElseIf line.Equals("%USERPROFILE%\SavedSearches") Then
                line = savedsearchespath
            ElseIf line.Equals("%USERPROFILE%\Videos") Then
                line = videospath
            ElseIf line.Contains("%USERNAME%") Then
                line = line.Replace("%USERNAME%", Username)
            Else

                Console.Writeline(line)

                'Read line and create a full path for the destination
                Dim SubPath As String = line.Split("\").Last
                Dim FullDestPath As String = Destpath & "\" & SubPath

                Console.Writeline(FullDestPath)

                If Not System.IO.Directory.Exists(FullDestPath) Then
                    System.IO.Directory.CreateDirectory(FullDestPath)
                End If


                'Get directory info's
                Dim SourceDir As DirectoryInfo = New DirectoryInfo(line)
                Dim DestDir As DirectoryInfo = New DirectoryInfo(FullDestPath)
                Dim ChildFile As FileInfo

                'Loop through each file in the SourceDir
                For Each ChildFile In SourceDir.GetFiles()

                    'Display file being copied
                    SetLabelText_ThreadSafe(Me.lblStatus, "Copying: " & line & "\" & ChildFile.Name & "")

                    'Do the copy
                    ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), True)

                    deststring = DestDir.ToString & "\" & ChildFile.Name
                    Dim sourcedirstring As String
                    sourcedirstring = SourceDir.ToString & "\" & ChildFile.Name

                    'Open Stream
                    Dim CopyStream As New FileStream(sourcedirstring, FileMode.Open, FileAccess.Read)
                    Dim NewStream As New FileStream(deststring, FileMode.CreateNew)
                    Dim Buffer(100000) As Byte
                    Dim BytesRead As Integer

                    'Try loop for each file
                    Try
                        Do
                            BytesRead = CopyStream.Read(Buffer, 0, Buffer.Length)
                            NewStream.Write(Buffer, 0, BytesRead)
                            PercentComplete = (NewStream.Length / CopyStream.Length * 100)
                            PercentComplete = Decimal.Round((PercentComplete), 2)
                            If PercentComplete > 100 Then
                                PercentComplete = "0"
                            End If

                            'if the file count is only 1 file then make both progress bars the same so that the current file and total are the same
                            If filecount = 1 Then
                                percentage = ((NewStream.Length + filetotalsofarcopied) / Overallsize.ToString * 100)
                                percentage = Decimal.Round((percentage), 2)
                                If percentage > 100 Then
                                    percentage = 0
                                End If
                                SetLabelText_ThreadSafe(Me.lblTotalProgress, "" & percentage & "%")
                            Else
                                Timer6.Start()
                                percentage2 = ((NewStream.Length + filetotalsofarcopied) / Overallsize.ToString * 100)
                                percentage2 = Decimal.Round((percentage2), 2)
                                If percentage2 > 100 Then
                                    percentage2 = 0
                                End If
                                SetLabelText_ThreadSafe(Me.lblTotalProgress, "" & percentage2 & "%")
                            End If

                            SetLabelText_ThreadSafe(Me.lblCurrentFileProgress, "" & PercentComplete & "%")

                            Dim time As Long = Label22.Text

                            'Calculate copy speed
                            Dim kbps As Double = (NewStream.Length + filetotalsofarcopied) / (time * 1024)
                            If kbps < 1024 Then
                                SetLabelText_ThreadSafe(Me.lblCopy, String.Format("Copy Speed: {0:0.00} KB/s", kbps))
                            Else
                                SetLabelText_ThreadSafe(Me.lblCopy, String.Format("Copy Speed: {0:0.00} MB/s", kbps / 1024))
                            End If

                        Loop Until BytesRead = 0 And PercentComplete = 100

                    Catch ex As Exception
                    Finally
                        CopyStream.Dispose()
                        NewStream.Dispose()
                    End Try

                    'File counter
                    int3 = int3 + 1

                    'Calculate data being moved for eta to completion
                    Dim filetotalbytes As Double = ChildFile.Length
                    filetotalsofarcopied = filetotalbytes + filetotalsofarcopied

                    'Check for pending cancel
                    If BackgroundWorker1.CancellationPending = True Then
                        BackgroundWorker1.CancelAsync()
                        Exit Sub
                    End If

                Next

                'Loop through Sub directories of SourceDir
                Dim SubDir As DirectoryInfo
                For Each SubDir In SourceDir.GetDirectories()
                    CopyDirectory(SubDir.FullName, Path.Combine(DestDir.FullName, SubDir.Name), Overwrite)
                Next
            End If

            ' Read in the next line.
            line = r.ReadLine

        Loop

    End Using

End Sub

更新:包含.text文件内容:

***DESTINATION***
D:\Test
***Sources***
%USERPROFILE%\Downloads
%USERPROFILE%\Favorites
D:\User Data\Adam\Documents\Test

1 个答案:

答案 0 :(得分:1)

您应该在前两个Ifs中删除对ReadLine的调用,并将其替换为Continue Do关键字。

一般来说,最好只在代码中调用一次ReadLine 例如,您可以使用StreamReader.Peek方法检查流结束。

最后,应在%USERNAME%检查后关闭If块

    ' removed
    ' line = r.ReadLine()

    Dim line As String
    Do While r.Peek <> -1
        line = r.ReadLine()
        Console.Writeline(line)

        If line.StartsWith("*") Then
            ' Not significant, skip to the next line
            Continue Do
        ElseIf line.Contains(Destpath) Then
            ' Not significant, skip to the next line
            Continue Do
        ElseIf line.Equals("%USERPROFILE%\Desktop") Then
            line = desktoppath
        ElseIf line.Equals("%USERPROFILE%\Downloads") Then
            line = downloadsPath
        ElseIf line.Equals("%USERPROFILE%\Contacts") Then
            line = contactspath
        ElseIf line.Equals("%USERPROFILE%\Documents") Then
            line = documentspath
        ElseIf line.Equals("%USERPROFILE%\Favorites") Then
            line = favoritespath
        ElseIf line.Equals("%USERPROFILE%\Links") Then
            line = linkspath
        ElseIf line.Equals("%USERPROFILE%\Music") Then
            line = Musicpath
        ElseIf line.Equals("%USERPROFILE%\Pictures") Then
            line = picturespath
        ElseIf line.Equals("%USERPROFILE%\SavedGames") Then
            line = savedgamespath
        ElseIf line.Equals("%USERPROFILE%\SavedSearches") Then
            line = savedsearchespath
        ElseIf line.Equals("%USERPROFILE%\Videos") Then
            line = videospath
        ElseIf line.Contains("%USERNAME%") Then
            line = line.Replace("%USERNAME%", Username)            
        End If

        ' Now your line variable should be always correct and            
        ' you can execute your copying logic 
        ......

        line = r.ReadLine
    Loop