Cocoa 中 FileSystemWatcher (.NET) 的等效项
我正在 Cocoa 中开发一个应用程序。 我想不断检查特定位置的文件内容是否已更改(例如 .NET 中的 FileSystemWatcher)。 请任何人给我一个解决方案
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
请查看 FSEvents。
Please have a look at FSEvents.
正如 Diederik 所说, FSEvents 是 Apple 的 Carbon API用于监听文件系统事件。 有人为 FSEvents 创建了一个名为 SCEvents 的 Cocoa/Objective-C 包装器,它有点小更容易使用。
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.
另一种选择是直接从 /dev/fsevents 消防水管喝水。 我正在开发一个应用程序来完成此任务并且效果非常好。 如果文件发生更改、删除、移动、属性更改等,您会收到通知。当然,这不是“Cocoa”选项,因为它主要是 C 代码,但我们在 Cocoa 应用程序中使用它。
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 很棒,但它们用于监视文件夹。 要监视单个文件,您需要查看 kqueues。 在终端中尝试“man kqueue”。
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.
查看 NSWorkspace 类文档。
Look at the NSWorkspace class documentation.
下面是如何使用书签和 NSFileManager 执行此操作的示例,以便您可以在文件移动时跟踪该文件。
https://github.com/ptrsghr/FileWatcher
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
有关 kqueues 的进一步资源是 http://developer.apple。 com/documentation/Darwin/Conceptual/FSEvents_ProgGuide/KernelQueues/KernelQueues.html
A further resource on kqueues is http://developer.apple.com/documentation/Darwin/Conceptual/FSEvents_ProgGuide/KernelQueues/KernelQueues.html