FileCopy循环不处理超过第一个文件

时间:2016-04-27 13:01:34

标签: vb.net filestream

我正在使用后台工作程序来运行FileCopy循环。所有路径都是正确的,但是当循环运行时,只处理要复制的第一个文件夹中的第一个文件。任何人都可以看到我错在哪里?我可以看到,因为Console.WriteLine("Next File about to start")没有开火。我认为问题出在Filestream循环中,但我看不出问题。有人能指出我正确的方向吗?

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()

                Console.WriteLine(ChildFile.FullName)

                '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.Append)
                Dim Buffer(100000) As Byte
                Dim BytesRead As Integer

                'Try loop for each file
                Try
                    Do
                        Timer6.Start()
                        BytesRead = CopyStream.Read(Buffer, 0, Buffer.Length)
                        NewStream.Write(Buffer, 0, BytesRead)


                        percentageTotal = ((NewStream.Length + filetotalsofarcopied) / Overallsize.ToString * 100)
                        percentageTotal = Decimal.Round((percentageTotal), 2)
                        If percentageTotal > 100 Then
                            percentageTotal = 0
                        End If

                        SetLabelText_ThreadSafe(Me.lblTotalProgress, "" & percentageTotal & "%")

                        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 percentageTotal = 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

                Console.WriteLine(filetotalsofarcopied)

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

                Console.WriteLine("Next File about to start")
            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

        Loop Until r.EndOfStream

    End Using

End Sub

0 个答案:

没有答案
相关问题