在Objective-C中观察文件或文件夹 [英] Observe a File or Folder in Objective-C

查看:190
本文介绍了在Objective-C中观察文件或文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

侦听文件夹或文件以查看文件夹或文件是否已保存或是否已添加新文件的最佳方式是什么?

What is the best way to listen to a folder or file to see if it has been saved or if a new file has been added?

推荐答案

FSEvents API是理想的,如果你只是想观看目录,但它不处理单个文件的监控。 Stu Connolly有一个伟大的Objective-C封装为FSEvents C API,它叫做SCEvents,你可以得到它在这里:

The FSEvents API is ideal if you just want to watch directories but it doesn't handle the monitoring of individual files. Stu Connolly has a great Objective-C wrapper for the FSEvents C API, it's called SCEvents and you can get it here:

http://stuconnolly.com/blog/scevents-011/

关于FSEvents的好处是,你只需要看一个文件夹,你会收到任何发生在该文件夹的子文件夹层次结构中的任何更改的通知。

The nice thing about FSEvents is that you just need to watch one folder and you will be notified of any changes that occur anywhere in the subfolder hierarchy of that folder.

如果你需要文件级通知,你需要使用kqueues。 Uli Kusterer有一个伟大的Objective-C包装器:

If you need file-level notifications you will need to use kqueues. Uli Kusterer has a great Objective-C wrapper:

http://zathras.de/angelweb/sourcecode.htm#UKKQueue

这两种方法都比C语言API。

Either of these methods is a lot easier than wrangling with the C APIs directly, which are not particularly well documented and a bit obtuse.

如果你需要支持Tiger,你需要使用kqueue作为FSEvents API没有正式可在10.4。

If you need to support Tiger you'll need to use kqueues as the FSEvents API wasn't officially available in 10.4.

这篇关于在Objective-C中观察文件或文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆