2011-12-08 11 views
8

robie strukturę folderów w folderze moim zasobów jak ... Zasób => MyData => S1 następnie w S1 => Name.png, data.pptJak uzyskać listę folderów i plików z folderu zasobów w telefonie iPhone?

Teraz chcę uzyskać wszystkie katalogi i nazwy plików . Tutaj nazwa MyData będzie tylko statyczna, inne mogą się zmieniać. Jak mogę dodać S1, S2, S3 i liczbę plików w tym. Jak odczytać te treści za pomocą C? Próbowałem poniżej kodu.

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); 
    } 

Dzięki

Odpowiedz

12

można uzyskać ścieżkę do katalogu, jak to Resources,

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

Następnie dołączyć Documents do ścieżki,

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

Następnie można użyj dowolnego z API listy katalogów o numerze NSFileManager.

NSError * error; 
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error]; 
Powiązane problemy