I've set up my filesystemwatcher, but I'm getting odd results. I've found that if I put a complete path of a directory, and set include subdirectories, then changes are channeled as they should. But what I've tried to do unsuccessfully is set my "C:\"
drive as the path with include subdirectories.
Are there restrictions on using the main drive as a path?
I've tried every syntax of it I could, but no success. Just wondering if there's something there I don't know...comments welcome :)
Here's pertinent code if anyone wants to take a look at it. Suggestions would be great:
<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
I moved the
watcher.EnableRaisingEvents = True
after the code that register the events as Jimi suggested. This solve the problem setting up the watched folders to register events as expected.