问题描述
我是Cocoa应用程序开发的新手。我希望我的应用程序被通知时,任何文件在给定的目录下被修改(文件夹监视器)。修改意味着删除,添加,文件内容被更改。我尝试使用FSEvents与使用NSWorkspace的通知中心或委托消息在UKKQueue在。我的应用程序得到通知,当目录下的任何文件被修改。但问题是它没有给出被修改的特定文件的名称或路径。
任何想法如何在特定文件中查看文件夹进行修改
FSEvent 通知文件夹内容已更改,您需要将您存储的有关文件夹内容的信息与实际的当前内容进行比较。
这可能是一个简单的变量array ivar命名为 folderContents ,其中包含一组文件属性字典。您可以使用 -attributesOfItemAtPath:error:方法从 NSFileManager 或其子集中返回的字典。 p>
当您收到文件夹通知时,您需要执行的操作遍历存储的字典,并检查是否添加,删除或修改了任何文件。 NSFileManager 属性字典包含您需要执行此操作的所有信息。
存储有关包含更新信息的文件夹的信息。
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??
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 of NSFileManager 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.
这篇关于如何获取文件夹更改通知(文件夹监视器)在可可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!