如何在沙箱中获取所有文档的文件名?

时间:2022-04-01 11:26:14

I have the following code:

我有以下代码:

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

However I am clueless as to how to get all the files names and assign them to an array?

但是我不知道如何获取所有的文件名称并将它们分配给一个数组?

Any help would be appreciated.

如有任何帮助,我们将不胜感激。

3 个解决方案

#1


11  

NSFileManager has a method called contentsOfDirectoryAtPath:error: that returns an array of all the files in that directory.

nsfilager有一个名为contentsofemandirectoryatpath:error的方法,它返回该目录中所有文件的数组。

You can use it like this:

你可以这样使用:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if([paths count] > 0)
{
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSError *error = nil;
    NSArray *documentArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
    if(error)
    {
        NSLog(@"Could not get list of documents in directory, error = %@",error);
    }
}

The documentsArray object will contain a list of all the files in that directory.

documentsArray对象将包含该目录中所有文件的列表。

#2


4  

Use NSFileManager's contentsOfDirectoryAtPath:error: method.

使用NSFileManager contentsOfDirectoryAtPath:错误:方法。

#3


1  

    NSError *error;
    NSFileManager* fileMgr = [NSFileManager defaultManager];
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSArray* documentsArray = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
    [documentsArray count];

Is another way of doing this for those with a different set up.

这是另一种方法,用于不同的设置。

#1


11  

NSFileManager has a method called contentsOfDirectoryAtPath:error: that returns an array of all the files in that directory.

nsfilager有一个名为contentsofemandirectoryatpath:error的方法,它返回该目录中所有文件的数组。

You can use it like this:

你可以这样使用:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if([paths count] > 0)
{
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSError *error = nil;
    NSArray *documentArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
    if(error)
    {
        NSLog(@"Could not get list of documents in directory, error = %@",error);
    }
}

The documentsArray object will contain a list of all the files in that directory.

documentsArray对象将包含该目录中所有文件的列表。

#2


4  

Use NSFileManager's contentsOfDirectoryAtPath:error: method.

使用NSFileManager contentsOfDirectoryAtPath:错误:方法。

#3


1  

    NSError *error;
    NSFileManager* fileMgr = [NSFileManager defaultManager];
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSArray* documentsArray = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
    [documentsArray count];

Is another way of doing this for those with a different set up.

这是另一种方法,用于不同的设置。