views:

1099

answers:

4

I find the .Net FileSystemWatcher class really handy for writing utilities that automatically come to life when files show up in their watched folders. Is there any equivalent to this functionality in the *nix world that would allow me to watch a folder (and possibly all of its subdirectories)?

Edit: Preferably this will be something that doesn't require kernel patches.

+7  A: 

That would be Gamin the File Alteration Monitor or Inotify.

Edit: Mono does have Gamin bindings - in fact, its implementation of FileSystemWatcher uses Gamin. See http://www.mono-project.com/FAQ:_Technical (search the page for FileSystemWatcher, the FAQ doesn't have anchors unfortunately.)

Mihai Limbășan
+2  A: 

Yes, dnotify and inotify.

I don't know if Mono has these wrapped, but it would be worth checking.

Barry Kelly
I've read elsewhere that the FileSystemWatcher doesn't work with linux filesystems when running in a Mono environment. Thanks though.
Luke
The problem with inotify is, that it's not portable b/c it only exists on linux and not on Unix in general.
André
Beagle uses inotify, so it's definitely wrapped in Mono somewhere.
Paul Fisher
+3  A: 

As has already being said, Mono has the class "System.IO.FileSystemWatcher", this is the relevant link: http://www.go-mono.com/docs/monodoc.ashx?link=T%3aSystem.IO.FileSystemWatcher

"Mono's implementation of the FileSystemWatcher has multiple backends. This is necessary because not all operating systems supported by Mono have all the features necessary to provide the functionality expected by applications.

If the operating system kernel supports watching directories (inotify on Linux, KEvents on BSD or OSX) that feature is used; Otherwise it falls back to using the Gamin or FAM libraries (these libraries provide an API to monitor directories) and if none of those features are available, Mono will poll every 750 milliseconds the directories watched.

You can force the polling behavior (instead of using the kernel support) by setting the MONO_MANAGED_WATCHER environment variable before executing your application. This might be useful for filesystems that do not support inotify and still require polling to detect changes."

Jordij
+2  A: 

If you're using the wonderful QT library (www.qtsoftware.com) it's included as the QFileSystemWatcher.