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
Please have a look at FSEvents.
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.
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.
Look at the NSWorkspace class documentation.
Here's an example of how to do it with bookmarks and NSFileManager so that you can follow the file if it's moved.
A further resource on kqueues is http://developer.apple.com/documentation/Darwin/Conceptual/FSEvents_ProgGuide/KernelQueues/KernelQueues.html
来源:https://stackoverflow.com/questions/1062679/equivalent-of-filesystemwatcher-net-in-cocoa