6

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

Monolo
  • 18,205
  • 17
  • 69
  • 103
MobX
  • 2,640
  • 7
  • 33
  • 54
  • 3
    Just 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 Answers7

10

Please have a look at FSEvents.

diederikh
  • 25,221
  • 5
  • 36
  • 49
6

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

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

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
  • 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
1

Look at the NSWorkspace class documentation.

1

Here's an example of how to do it with bookmarks and NSFileManager so that you can follow the file if it's moved.

https://github.com/ptrsghr/FileWatcher

rgbrgb
  • 1,146
  • 1
  • 11
  • 17