对filesystemwatcher路径的限制?

时间:2018-09-18 03:47:50

标签: vb.net filesystemwatcher

我已经设置了 filesystemwatcher ,但是结果却很奇怪。我发现,如果我放置目录的完整路径,并设置包括子目录,那么更改将按需进行。但是,我尝试失败的操作是将"C:\"驱动器设置为包含include子目录的路径。

使用主驱动器作为路径是否受到限制?

我尝试了所有可能的语法,但没有成功。只是想知道那里是否有我不知道的东西...欢迎评论:)

如果有人想看一下这里的相关代码。建议会很棒:

<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

1 个答案:

答案 0 :(得分:0)

我移动了

  

watcher.EnableRaisingEvents =真

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