2011-12-08 36 views
8

我在我的資源文件夾做文件夾結構,就像...資源=>邁德特=> S1 然後在S1 => Name.png,data.ppt如何從iPhone資源文件夾中獲取文件夾和文件的列表?

現在,我想所有的文件夾列表和文件名。這裏MyData的名字將只是靜態的,其他的可能會改變。就像我可以添加S1,S2,S3和其中的文件數量一樣。那麼如何通過Objective C來閱讀這些內容呢?我嘗試了下面的代碼。

NSString *bundlePathName = [[NSBundle mainBundle] bundlePath]; 
    NSString *dataPathName = [bundlePathName stringByAppendingPathComponent:@"Resources"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:dataPathName]) { 
     NSLog(@"%@ exists", dataPathName); 
     BOOL isDir = NO; 
     [fileManager fileExistsAtPath:dataPathName isDirectory:(&isDir)]; 
     if (isDir == YES) { 
      NSLog(@"%@ is a directory", dataPathName); 
      NSArray *contents; 
      contents = [fileManager contentsOfDirectoryAtPath:dataPathName error:nil]; 
      for (NSString *entity in contents) { 
       NSLog(@"%@ is within", entity); 
      } 
     } else { 
      NSLog(@"%@ is not a directory", dataPathName); 
     } 
    } else { 
     NSLog(@"%@ does not exist", dataPathName); 
    } 

感謝,

回答

12

你可以到Resources目錄這樣的路徑,

NSString * resourcePath = [[NSBundle mainBundle] resourcePath]; 

然後追加Documents的路徑,

NSString * documentsPath = [resourcePath stringByAppendingPathComponent:@"Documents"]; 

然後你可以使用的任何目錄列表API。

NSError * error; 
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error]; 
相關問題