对 filesystemwatcher 路径的限制?

Restrictions on filesystemwatcher paths?

我已经设置了我的 filesystemwatcher,但我得到了奇怪的结果。我发现,如果我放置一个目录的完整路径,并设置包含子目录,那么更改就会按照它们应该的方式进行引导。但是我尝试做的是将我的 "C:\" 驱动器设置为包含子目录的路径,但没有成功。

主盘做路径有限制吗?

我已经尝试了所有可能的语法,但没有成功。只是想知道是否有什么我不知道的......欢迎评论:)

如果有人想看一下,这里有相关代码。建议很好:

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")>
Private Sub WatchFolders()
    Dim myConn As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\kgene\source\repos\Test Program\Test Program\Resources\BakDb.accdb")

    MessageBox.Show("setting up watch folders")
    watcher = New FileSystemWatcher With {.NotifyFilter = NotifyFilters.DirectoryName Or NotifyFilters.FileName Or NotifyFilters.Attributes}
    watcher.Path = "c:\"
    watcher.IncludeSubdirectories = True
    watcher.EnableRaisingEvents = True

    AddHandler watcher.Changed, AddressOf LogChange
    AddHandler watcher.Created, AddressOf LogChange
    AddHandler watcher.Deleted, AddressOf LogChange
    AddHandler watcher.Renamed, AddressOf LogChangeR

End Sub

Private Sub LogChange(ByVal source As Object, ByVal e As FileSystemEventArgs)
    Dim myConn As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\kgene\source\repos\Test Program\Test Program\Resources\BakDb.accdb")
    Dim cmd As New OleDbCommand

    chkCnt = 0

    Try
        str = "SELECT Base, Target FROM Backup"
        cmd.Connection = myConn
        cmd.CommandText = str
        myConn.Open()
        Dim lstReader As OleDbDataReader = cmd.ExecuteReader()
        If cntPlns = 0 Then
            myConn.Close()
            Exit Sub
        End If
        lstReader.Read()
        Do Until chkCnt = cntPlns
            If lstReader.Item(0).ToString().ToLower() = Path.GetDirectoryName(e.FullPath).ToString.ToLower() Or lstReader.Item(1).ToString().ToLower() = Path.GetDirectoryName(e.FullPath).ToString().ToLower() Then
                If e.ChangeType = IO.WatcherChangeTypes.Changed Then
                    MessageBox.Show("first item changed")
                End If
                If e.ChangeType = IO.WatcherChangeTypes.Created Then
                    MessageBox.Show("first item create")
                End If
                If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
                    MessageBox.Show("first item delete")
                End If
            End If
            If cntPlns = 0 Then
                myConn.Close()
                Exit Sub
            End If
            lstReader.Read()
            chkCnt += 1
        Loop
        chkCnt = 0
        myConn.Close()
    Catch ex As Exception
        chkCnt = 0
        myConn.Close()
        MessageBox.Show("There was a problem conncecting to the database to watch your folders")
        Exit Sub
    End Try
End Sub

我移动了

watcher.EnableRaisingEvents = True

在按照 Jimi 的建议注册事件的代码之后。这解决了设置监视文件夹以按预期注册事件的问题。