如何读取文档目录中存在的文件列表

时间:2023-02-06 12:20:51

I have some Dynamically created XML file present in my Document directory.So i want to read and store all these file name in to a NSMutableArray.

我在Document目录中有一些动态创建的XML文件。所以我想读取并将所有这些文件名存储到NSMutableArray中。

How Can i do this?

我怎样才能做到这一点?

4 个解决方案

#1


4  

NSString* documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
NSError* error = nil;
NSArray* fileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectory error:&error];

#2


1  

Something like this?

像这样的东西?

for (NSString *file in [[NSFileManager defaultManager]
                        contentsOfDirectoryAtPath:@"/path/documents"
                        error:NULL]) {
   // do something with file
}

#3


1  

Have a look at NSFileManager.

看看NSFileManager。

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error

will give you the list of files.

会给你一个文件列表。

#4


1  

Use this.. This will give you all file names from documents directory..

使用此..这将为您提供文档目录中的所有文件名。

   NSString* documentDirectory = [NSSearchPathForDirectoriesInDomains
                      (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  

   NSError* error = nil;
   NSArray* filesInDocuments = [[NSFileManager defaultManager] 
                   contentsOfDirectoryAtPath:documentDirectory error:&error];

   for(int k = 0; k < [filesInDocuments count]; k++)
  {
    NSString *filename = [filesInDocuments objectAtIndex:k];

    [namesArray addObject:filename];
   }

#1


4  

NSString* documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
NSError* error = nil;
NSArray* fileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectory error:&error];

#2


1  

Something like this?

像这样的东西?

for (NSString *file in [[NSFileManager defaultManager]
                        contentsOfDirectoryAtPath:@"/path/documents"
                        error:NULL]) {
   // do something with file
}

#3


1  

Have a look at NSFileManager.

看看NSFileManager。

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error

will give you the list of files.

会给你一个文件列表。

#4


1  

Use this.. This will give you all file names from documents directory..

使用此..这将为您提供文档目录中的所有文件名。

   NSString* documentDirectory = [NSSearchPathForDirectoriesInDomains
                      (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  

   NSError* error = nil;
   NSArray* filesInDocuments = [[NSFileManager defaultManager] 
                   contentsOfDirectoryAtPath:documentDirectory error:&error];

   for(int k = 0; k < [filesInDocuments count]; k++)
  {
    NSString *filename = [filesInDocuments objectAtIndex:k];

    [namesArray addObject:filename];
   }