MobX
MobX

Reputation: 2670

Equivalent of FileSystemWatcher (.NET) in Cocoa

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

Upvotes: 6

Views: 3000

Answers (7)

rgbrgb
rgbrgb

Reputation: 1166

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

Upvotes: 1

Tim Bedford
Tim Bedford

Reputation:

Look at the NSWorkspace class documentation.

Upvotes: 1

Dave DeLong
Dave DeLong

Reputation: 243156

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.

Upvotes: 2

Jon Hess
Jon Hess

Reputation: 14247

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.

Upvotes: 0

Bryan Traywick
Bryan Traywick

Reputation: 236

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.

Upvotes: 6

diederikh
diederikh

Reputation: 25281

Please have a look at FSEvents.

Upvotes: 10

Related Questions