|
-
Oct 17th, 2010, 10:42 AM
#1
Thread Starter
Addicted Member
[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.
-
Oct 17th, 2010, 04:49 PM
#2
Thread Starter
Addicted Member
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.
-
Oct 17th, 2010, 05:18 PM
#3
Re: FileSystemWatcher not detecting change in file
Are you refilling the dataset?
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Oct 17th, 2010, 05:35 PM
#4
Thread Starter
Addicted Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|