Connecting Tech Pros Worldwide Forums | Help | Site Map

C# Application FileSystemWatcher to handle replaced files

Newbie
 
Join Date: Oct 2008
Posts: 19
#1: Oct 8 '08
I am using FileSystemWatcher to monitor a directory for files that are created and replaced. It is working fine for new files, but if I copy a file into the directory with the same name and choose to replace the existing file, it doesn't trigger any event.

Does anyone know how to get that specific event? (Replace a file)

Thanks.

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#2: Oct 8 '08

re: C# Application FileSystemWatcher to handle replaced files


I would expect that it triggers the 'Changed' event. I see that every time I add a line to a text log file. There is a change in the 'last access time' so the file has a change.

I would expect that totally replacing the file must register as a change.
Newbie
 
Join Date: Oct 2008
Posts: 19
#3: Oct 8 '08

re: C# Application FileSystemWatcher to handle replaced files


I have tried making the NotifyFilter = LastWrite, and added an event handler for Changed, but that is still not picking up replaced files (The new files have new Date Modified values).

Expand|Select|Wrap|Line Numbers
  1. oFileWatcher.NotifyFilter = NotifyFilters.LastWrite;
  2.  
  3. oFileWatcher.Deleted += new System.IO.FileSystemEventHandler(Deleted);
  4. oFileWatcher.Created += new System.IO.FileSystemEventHandler(Created);
  5. oFileWatcher.Changed += new System.IO.FileSystemEventHandler(Changed);
Any other ideas?
Newbie
 
Join Date: Oct 2008
Posts: 19
#4: Oct 8 '08

re: C# Application FileSystemWatcher to handle replaced files


I got it to work with the above code, but for every replaced file, it fires 3 Changed events.....
Newbie
 
Join Date: Oct 2008
Posts: 6
#5: Oct 8 '08

re: C# Application FileSystemWatcher to handle replaced files


It's the Chaged event that gets fired in this scenario, have set EnableRaisingEvents to true ?
Newbie
 
Join Date: Oct 2008
Posts: 6
#6: Oct 8 '08

re: C# Application FileSystemWatcher to handle replaced files


This is as described in MSDN, I'd guess your getting one for each attribute that changes. Try changing the NotifyFilter to get just the one. I think you'll always get more than one for a replace though.
Reply