如何在可可中获取文件夹更改通知(文件夹观察器)

发布于 2024-12-21 19:55:31 字数 376 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗笺 2024-12-28 19:55:31

您必须编写代码来跟踪文件夹的内容,然后每当您收到文件夹内容已更改的 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 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.

爱情眠于流年 2024-12-28 19:55:31

NSMetadataQuery 非常适合监视文件夹:

- (void)setupWatchedFolder {
    NSString *watchedFolder = @"/path/to/foo";

    NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
    [query setSearchScopes:@[watchedFolder]];
    [query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE '*.*'", NSMetadataItemFSNameKey]];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(queryFoundStuff:) name:NSMetadataQueryDidFinishGatheringNotification object:query];
    [nc addObserver:self selector:@selector(queryFoundStuff:) name:NSMetadataQueryDidUpdateNotification object:query];

    [query startQuery];
}

- (void)queryFoundStuff:(NSNotification *)notification {

    NSMetadataQuery *query = self.metadataQuery;
    [query disableUpdates];

    NSMutableArray *results = [NSMutableArray arrayWithCapacity:self.metadataQuery.resultCount];

    for (NSUInteger i=0; i<self.metadataQuery.resultCount; i++) {
        [results addObject:[[self.metadataQuery resultAtIndex:i] valueForAttribute:NSMetadataItemPathKey]];
    }

    // do something with you search results
    // self.results = results;

    [query enableUpdates];
}

NSMetadataQuery works well for watching folders:

- (void)setupWatchedFolder {
    NSString *watchedFolder = @"/path/to/foo";

    NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
    [query setSearchScopes:@[watchedFolder]];
    [query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE '*.*'", NSMetadataItemFSNameKey]];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(queryFoundStuff:) name:NSMetadataQueryDidFinishGatheringNotification object:query];
    [nc addObserver:self selector:@selector(queryFoundStuff:) name:NSMetadataQueryDidUpdateNotification object:query];

    [query startQuery];
}

- (void)queryFoundStuff:(NSNotification *)notification {

    NSMetadataQuery *query = self.metadataQuery;
    [query disableUpdates];

    NSMutableArray *results = [NSMutableArray arrayWithCapacity:self.metadataQuery.resultCount];

    for (NSUInteger i=0; i<self.metadataQuery.resultCount; i++) {
        [results addObject:[[self.metadataQuery resultAtIndex:i] valueForAttribute:NSMetadataItemPathKey]];
    }

    // do something with you search results
    // self.results = results;

    [query enableUpdates];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文