[RESOLVED] FileSystemWatcher not detecting change in file
Hey there,
I've written the following code. It works fine for creating, deleting and renaming files. However when I set a file to read only, or modify the file (i.e open it in notepad and change the contents) nothing happens (it doesn't hit the change breakpoint)
I have the following code:
VB.NET Code:
Private fsw As FileSystemWatcher
Public Sub WatchFiles(ByVal tblMappings As DataSet)
For Each row As DataRow In tblMappings.Tables(0).Rows
Try
fsw = New FileSystemWatcher
Dim FolderPath As String = row.Item(2)
If Directory.Exists(FolderPath) Then
fsw.Path = FolderPath
fsw.NotifyFilter = IO.NotifyFilters.DirectoryName
fsw.NotifyFilter = fsw.NotifyFilter Or IO.NotifyFilters.FileName
fsw.NotifyFilter = fsw.NotifyFilter Or IO.NotifyFilters.Attributes
fsw.NotifyFilter = fsw.NotifyFilter Or IO.NotifyFilters.LastWrite
fsw.NotifyFilter = fsw.NotifyFilter Or IO.NotifyFilters.Size
fsw.NotifyFilter = fsw.NotifyFilter Or IO.NotifyFilters.LastAccess
fsw.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.DirectoryName Or NotifyFilters.FileName Or NotifyFilters.CreationTime
fsw.IncludeSubdirectories = False
fsw.EnableRaisingEvents = True
AddHandler fsw.Changed, AddressOf fsw_changed
AddHandler fsw.Created, AddressOf fsw_created
AddHandler fsw.Renamed, AddressOf fsw_renamed
AddHandler fsw.Deleted, AddressOf fsw_deleted
fsw.EnableRaisingEvents = True
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Next
End Sub
Private Sub fsw_changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
Dim db As New DatabaseFunctions
db.AddToLog(DatabaseFunctions.PendingType.Change, e.FullPath, GetServerFolderFromPCFolder(ftp.GetFolder(e.FullPath), _tblMappings), String.Empty, _strUsername, _strPassword, _strDomain, _strAusername, _tblMappings)
End Sub
Does anybody have any ideas as to whats going wrong?
Regards,
Adam.
Re: FileSystemWatcher not detecting change in file
Hmm so it seems to hit this if I move a file (moving a file does change - delete - delete - create). So it's not a question of the actual plumbing of the code. I think it is something to do with the notifyfilter.
Adam.
Re: FileSystemWatcher not detecting change in file
Are you refilling the dataset?
Re: FileSystemWatcher not detecting change in file
Hey!
Have just discovered that the issue is with this line:
fsw.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.DirectoryName Or NotifyFilters.FileName Or NotifyFilters.CreationTime
Instead of using &= I've used =. Which means that all the filters (i.e size and lastwrite) that i set before were scrapped and replaced with these! Silly mistake I know!
Thanks :)
Adam.