排除仍在IO.Directory.GetFiles(Path).Length中写入的文件

时间:2016-08-17 11:25:34

标签: vb.net

我正在使用Filesystemwatcher通过进度条跟踪Directorycopy到USB记忆棒的进度。这是我的代码:

Public counterLocalFiles As String
Private Sub CopyUSB
        fsw_USB = New IO.FileSystemWatcher(Path)
        fsw_USB.IncludeSubdirectories = True
        fsw_USB.EnableRaisingEvents = True
        UpdateProgressBarMaximum2(CInt(counterLocalFiles))
        counterLocalFiles = System.IO.Directory.GetFiles(SourcePath, 
        My.Computer.FileSystem.CopyDirectory(SourcePath, Path)"*.jpg*", SearchOption.AllDirectories).Length    
End Sub

Private Sub fsw_USB_Created(sender As Object, e As FileSystemEventArgs) Handles fsw_USB.Created
        Dim counterUSBFiles = IO.Directory.GetFiles(Path, "*.jpg", SearchOption.AllDirectories).Length
        UpdateProgressBar2(CInt(counterUSBFiles))
        UpdateLabelText2(CStr(counterUSBFiles) & " von " & CStr(counterLocalFiles))
End Sub

Private Sub UpdateProgressBarMaximum2(Maximum As Int32)
    If ProgressBar2.InvokeRequired Then
        ProgressBar2.Invoke(New Action(Of Integer)(AddressOf UpdateProgressBarMaximum2), Maximum)
    Else
        'We are on the UI thread so update the control.
        ProgressBar2.Maximum = Maximum
    End If
End Sub

Private Sub UpdateProgressBar2(Value As Int32)
    If ProgressBar2.InvokeRequired Then
        ProgressBar2.Invoke(New Action(Of Integer)(AddressOf UpdateProgressBar2), Value)
    Else
        'We are on the UI thread so update the control.
        ProgressBar2.Value = Value
    End If
End Sub

我现在的问题是,我使用的USB 2.0 Stick有点慢。现在,Directory副本实际上首先创建了所有文件(我猜它是某种多重处理的副本),然后写入它们。无论文件的大小如何,SystemFileWatcher都会触发,计数器会上升。因此,当progressBar已满时,我们距离实际完成复制到usb驱动器还有很长的路要走。任何人都知道如何解决这个问题?

0 个答案:

没有答案