Equivalent of FileSystemWatcher (.NET) in Cocoa

非 Y 不嫁゛ 提交于 2019-12-04 05:28:21

Please have a look at FSEvents.

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.

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.

https://github.com/ptrsghr/FileWatcher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!