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
-
3Just a suggestion: "give me a solution" could be interpreted as coming off slightly abrasive in English, and also implies that you may not have looked for an answer for yourself. It's good form to Google before posting a question. :-) – Quinn Taylor Jun 30 '09 at 16:32
7 Answers
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.

- 236
- 1
- 3
-
1As a pedantic aside, FSEvents is not a Carbon API; it is part of the Core Services framework. – briankc Jun 18 '11 at 02:13
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.

- 242,470
- 58
- 448
- 498
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.

- 14,237
- 1
- 47
- 51
-
SCEvents is the better option for someone looking for a Cocoa solution, though. – Quinn Taylor Jun 30 '09 at 16:29
-
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 Aug 07 '09 at 21:52
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.

- 1,146
- 1
- 11
- 17
A further resource on kqueues is http://developer.apple.com/documentation/Darwin/Conceptual/FSEvents_ProgGuide/KernelQueues/KernelQueues.html

- 4,104
- 1
- 23
- 21