views:

247

answers:

3

Hi,

I'm writing a mini editor component that is much like Notepad++ or UltraEdit that needs to monitor the files the users open - its a bit slimy, but thats the way it needs to be.

Is it wise to use multiple instances of FileSystemWatcher to monitor the open files - again like Notepad++ or UltraEdit or is there a better way to manage these?

They'll be properly disposed once the document has been closed.

Sorry, one other thing, would it be wiser to create a generic FileSystemWatcher for the drive and monitor that, then only show them a message to reload the file once I know its the right file? Or is that retarted?

+3  A: 

You're not going to run into problems with multiple FileSystemWatchers, and there really isn't any other way to pull this off.

For performance, just be sure to specify as narrow filters as you can get away with.

Kevin Montrose
A: 

It depends on the likely use cases.

If a user is going to open several files in the same directory and likely not modify anything else a single watcher for that directory may be less onerous than one per file if the number of files is large.

The only way you will find out is by benchmarking. Certainly doing one per file makes the lifespan of the watcher much simpler so that should be your first approach. Note that the watchers fire their events on a system thread pool, so multiple watchers can fire at the same time (something that may influence you design)

I certainly wouldn't do a watcher per drive, you will cause far more effort that way even with aggressive filtering.

ShuggyCoUk
+1  A: 

FileSystemWatcher have a drawback, it locks watched folder, so, for example, if you are watching file on removable storage, it prevent "safe device removal".

You can try using Shell Notifications via SHChangeNotifyRegister. In this case you will have one entry point for all changes (or several if you want to), but in this case you will need some native shell interop.

arbiter