2010-03-29 13 views
5

का उपयोग कर निर्देशिका की अनुमति को कैसे बदलें, मैं वर्तमान में निर्देशिका की अनुमति बदलने के लिए NSFileManager setAttributes का उपयोग कर रहा हूं। मेरी समस्या यह है कि यह इतनी बार ऐसा नहीं लगता है। क्या ऐसा करने के लिए मजबूर करने का कोई तरीका है?NSFileManager setAttributes

उत्तर

4

मुझे नहीं लगता कि यह करने के लिए निर्मित एक विधि है, लेकिन ऐसा लगता है कुछ करने के लिए मुश्किल नहीं होना चाहिए:

NSString *path = @"/The/root/directory"; 
NSDictionary *attributes; // Assume that this is already setup 


NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; 
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 
     } 
    } 
} 

यह untested है, लेकिन आप एक प्रारंभिक बिंदु देना चाहिए।

2
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]; 
    } 
} 
संबंधित मुद्दे