首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用NSFileManager setAttributes递归更改目录的权限

如何使用NSFileManager setAttributes递归更改目录的权限
EN

Stack Overflow用户
提问于 2010-03-29 13:47:12
回答 2查看 4.3K关注 0票数 5

我当前正在使用NSFileManager setAttributes更改目录的权限。我的问题是,它似乎不会递归地这样做。有没有办法迫使它这么做呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-03-29 13:56:51

我不认为有一个内置的方法可以做到这一点,但它应该不会很难做到如下所示:

代码语言:javascript
复制
NSString *path = @"/The/root/directory";
NSDictionary *attributes;   // Assume that this is already setup


NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *subPaths = [fileManager subpathsAtPath:path];
for (NSString *aPath in subPaths) {
    BOOL isDirectory;
    [fileManager fileExistsAtPath:aPath isDirectory:&isDirectory];
    if (isDirectory) {
        // Change the permissions on the directory here
        NSError *error = nil;
        [fileManager setAttributes:attributes ofItemAtPath:aPath error:&error];
        if (error) {
            // Handle the error
        }
    }
}

这是未经测试的,但应该给您一个起点。

票数 4
EN

Stack Overflow用户

发布于 2010-05-27 05:28:10

代码语言:javascript
复制
NSString *path = @"/User/user/aPath";
NSFileManager *manager = [[[NSFileManager alloc] init] autorelease];
if ([manager fileExistsAtPath: path]) {
  NSDictionary *attrib = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithInt:0], NSFileGroupOwnerAccountID,
                             [NSNumber numberWithInt:0], NSFileOwnerAccountID,
                             @"root", NSFileGroupOwnerAccountName,
                             @"root", NSFileOwnerAccountName, nil ];
  NSError *error = nil;
  [manager setAttributes:attrib ofItemAtPath:path error:&error];
  NSDirectoryEnumerator *dirEnum = [manager enumeratorAtPath: path];
  NSString *file;
  while (file = [dirEnum nextObject]) {
    [manager setAttributes:attrib ofItemAtPath:[path stringByAppendingPathComponent:file] error:&error];
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2535984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档