使用FSWATCHER跟踪复制到外部驱动器的文件

时间:2018-07-11 09:44:28

标签: vb.net

我正在vb.net中开发一个程序,以使用fswatcher类监视并保留所有复制文件的记录。该程序运行良好,没有任何错误,但运行后没有输出。 该程序应该在运行后显示复制到RichTextBox文件的列表。 我愿意接受相同的建议,谢谢。

Imports System.IO
Imports System.Diagnostics
Public Class Form1
Public watchfolder As FileSystemWatcher
Dim watchers As FileSystemWatcher()

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btn_startwatch.Click
    watchfolder = New System.IO.FileSystemWatcher()
    Dim drives As String() = System.IO.Directory.GetLogicalDrives()
    watchers = New FileSystemWatcher(drives.Length - 1) {}
    Dim i As Integer = 0
    For Each strDrive As String In drives
        Dim df As New DriveInfo(strDrive)
        If Not df.IsReady Then
            Continue For
        End If
        watchfolder.Path = strDrive
        watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.CreationTime
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.LastAccess
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Security
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.LastWrite
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Size
        AddHandler watchfolder.Changed, AddressOf logchange
        AddHandler watchfolder.Created, AddressOf logchange
        AddHandler watchfolder.Deleted, AddressOf logchange
        AddHandler watchfolder.Renamed, AddressOf logrename
        watchfolder.EnableRaisingEvents = True
        btn_startwatch.Enabled = False
        btn_stop.enabled = True
    Next
End Sub

Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
    If e.ChangeType = IO.WatcherChangeTypes.Changed Then
        folderactivity.Text &= "file" & e.FullPath & "has been modified" & vbCrLf
    End If
    If e.ChangeType = IO.WatcherChangeTypes.Created Then
        folderactivity.Text &= "file" & e.FullPath & "has been created" & vbCrLf
    End If
    If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
        folderactivity.Text &= "file" & e.FullPath & "has been deleted" & vbCrLf
    End If
End Sub

Private Sub logrename(ByVal source As Object, ByVal e As  System.IO.RenamedEventArgs)
     folderactivity.Text &= "file" & e.OldName & "has been renamed to" & e.Name & vbCrLf
End Sub

Private Sub btn_stop_Click(sender As Object, e As EventArgs) Handles        btn_stop.Click
     watchfolder.EnableRaisingEvents = False
     btn_startwatch.Enabled = True
     btn_stop.Enabled = False
 End Sub

0 个答案:

没有答案