views:

502

answers:

6

Hai all,

I am developing an application in Cocoa. I want to constantly check whether the contents of a file in a particular location is changed or not (like FileSystemWatcher in .NET). Please anyone give me a solution

+3  A: 

Please have a look at FSEvents.

Diederik Hoogenboom
+1  A: 

As Diederik says, FSEvents is Apple's Carbon API for listening to file system events. Someone has created a Cocoa/Objective-C wrapper for FSEvents called SCEvents that is a little easier to use.

Bryan Traywick
+1  A: 

FSEvents are great, but they're used to monitor folders. To monitor a single file you'll want to check out kqueues. Try "man kqueue" in the terminal.

Jon Hess
SCEvents is the better option for someone looking for a Cocoa solution, though.
Quinn Taylor
FSEvents are still for directory changes though, and it sounds like SCEvents is a wrapper for FSEvents. The question asks to monitor a single file. Kqueues are much better suited for that. The link to SCEvents above also references UKKQueue, which might be a good wrapper too.
Jon Hess
+1  A: 

Another option would be to drink directly from the /dev/fsevents firehose. I work on an application that does exactly this and it works very well. You can be notified if a file changes, is deleted, is moved, has attributes changed, etc. Granted, this isn't a "Cocoa" option since it's mostly C code, but we're using this in a Cocoa app.

Dave DeLong
A: 

Look at the NSWorkspace class documentation.