如何在可可中获取文件夹更改通知(文件夹观察器)
我是 Cocoa 应用程序开发的新手。我希望我的应用程序在给定目录下的任何文件被修改时收到通知(文件夹观察程序)。修改是指删除、添加、更改文件内容。我尝试将 FSEvents 与 NSWorkspace 的通知中心或委托消息一起使用,如 UKKQueue 中 http:// www.zathras.de/angelweb/sourcecode.htm#UKKQueue。当目录下的任何文件被修改时,我的应用程序会收到通知。但问题是它没有给出被修改的特定文件的名称或路径。它给出目录的路径,但不给出特定文件的路径。
知道如何监视文件夹中特定文件的修改吗?
I am new to Cocoa Application development. I want my application to be notified when any file under a given directory is modified(folder watcher). Modified means deleted, added, content of file is changed. I tried using FSEvents also with using NSWorkspace's notification center or delegate messages as in UKKQueue at http://www.zathras.de/angelweb/sourcecode.htm#UKKQueue. My application got notification when any file under directory is modified. But the problem is that its not giving name or path of specific file which is modified. It gives path of directory but not path of specific file.
Any idea how can I watch folder for modification in specific file??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须编写代码来跟踪文件夹的内容,然后每当您收到文件夹内容已更改的
FSEvent
通知时,您需要将存储的有关文件夹内容的信息与实际的信息进行比较,当前内容。这可以是一个简单的可变数组 ivar,其名称类似于
folderContents
,其中包含一组文件属性字典。您可以使用从NSFileManager
的-attributesOfItemAtPath:error:
方法返回的字典或其子集。当您收到文件夹通知时,您所需要做的就是遍历存储的字典并检查是否已添加、删除或修改任何文件。 NSFileManager 属性字典包含执行此操作所需的所有信息。
然后,您需要使用更新后的信息来更新您存储的有关该文件夹的信息。
You have to write code to keep track of the contents of the folder and then whenever you receive an
FSEvent
notification that the folder contents have changed, you need to compare your stored information about the folder contents with the actual, current contents.This could be something as simple as a mutable array ivar named something like
folderContents
, which contains a set of file attributes dictionaries. You could use the dictionary returned from the-attributesOfItemAtPath:error:
method ofNSFileManager
or a subset of it.All you'd need to do when you receive a folder notification is iterate through the stored dictionaries and check to see whether any files have been added, removed or modified. The
NSFileManager
attributes dictionary contains all the info you need to do this.You'd then need to update your stored information about the folder with the updated information.
NSMetadataQuery 非常适合监视文件夹:
NSMetadataQuery works well for watching folders: