I have two applications, CREATOR
(which I can't modify) and OBSERVER
. CREATOR manipulates many files, and I need OBSERVER to know when that happens. I wrote OBSERVER in C#, and I'm using FileSystemWatcher
. I set path to my path, set filter to FILE
and add all the necessary events.开发者_JS百科 But when CREATOR modifies the file, no event is raised in OBSERVER. Oddly, when I modify the file by hand, OBSERVER does see the change. I thought that maybe CREATOR doesn't free the file, but when I close CREATOR, OBSERVER still doesn't see the change.
Any idea what I'm doing wrong?
Extra details: When CREATOR modifies the file, I can delete it by hand, or when I open the file I see that all changes are saved.
edit
my fileSystemWatcher object setting:
fileSystemWatcherObs.EnableRaisingEvents = true;
fileSystemWatcherObs.Filter = "kbd.dbf";
fileSystemWatcherObs.IncludeSubdirectories = true;
fileSystemWatcherObs.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Attributes |
NotifyFilters.CreationTime | NotifyFilters.DirectoryName |NotifyFilters.LastAccess | NotifyFilters.Security |
NotifyFilters.Size;
fileSystemWatcherObs.Path = "D:\\FOLDER";
fileSystemWatcherObs.SynchronizingObject = this;
fileSystemWatcherObs.Changed += new System.IO.FileSystemEventHandler( this.fileSystemWatcherObs_Changed );
fileSystemWatcherObs.Created += new System.IO.FileSystemEventHandler( this.fileSystemWatcherObs_Created );
fileSystemWatcherObs.Deleted += new System.IO.FileSystemEventHandler( this.fileSystemWatcherObs_Deleted );
fileSystemWatcherObs.Renamed += new System.IO.RenamedEventHandler( this.fileSystemWatcherObs_Renamed );
and of course method for this events
Here a few hints that maybe helps you:
- It doesn't matter if you made the changes
by hand
or by another application "you CREATOR", since when you make them by hand, you actually use some application like "notepad.exe" or something, so that is not really matters. - You should set
EnableRaisingEvent
to true "start watching" after you set the path and register the event handlers, so the it should be last thing after set all the configurations of the watcher. - At your
NotifyFilter
are setting the whole notifications filters which will leads you to receive duplicates notifications some times. - Since you only want to watch a specific folder, then you don't have to include sub folders, i.e
fIncludeSubdirectories
should be false. - Why you are setting a
SynchronizingObject
tothis
? a better if you have to anyway is to take it to anew object()
.
精彩评论