2011-12-08 14 views
8

私は私のリソースフォルダ内のフォルダ構造をしていますように...リソース=>のMyData => S1 その後、S1 => Name.png、data.pptiPhoneのリソースフォルダからフォルダとファイルのリストを取得するにはどうすればよいですか?

に今、私はすべてのフォルダリストとファイル名を取得したいです。ここでMyDataの名前は静的な他のものに変更されることがあります.S1、S2、S3、およびその中のファイルの数を追加することができます。どのようにObjective Cを通じてこれらのコンテンツを読むのですか?私は以下のコードを試しました。あなたはこのようなResourcesディレクトリへのパスを取得することができ

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

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

次にパスにDocumentsを追加し、

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

次に、あなたがすることができますのディレクトリリストAPIを使用してください。

NSError * error; 
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error]; 
関連する問題