如何获取Documents目录中文件的所有路径?

时间:2023-01-14 23:25:51

I usually get paths by using NSBundle.

我通常使用NSBundle来获取路径。

But, NSBundle does not contain Documents folder.

但是,NSBundle不包含Documents文件夹。

How to get All paths(or names) for files in Documents directory?

如何获取Documents目录中文件的所有路径(或名称)?

3 个解决方案

#1


30  

This will give u the paths for all files under documents directory

这将为您提供文档目录下所有文件的路径

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

    NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];

    NSLog(@"files array %@", filePathsArray);

You can get the path for each object in filePathsArray by using the below code

您可以使用以下代码获取filePathsArray中每个对象的路径

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:0]]; 

#2


0  

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

To get files from that, you have to give the path structure...

要从中获取文件,您必须提供路径结构...

    NSString *fileName = @"default.png";
    NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"%@", fileName];

The folderPath will return you the location of given file name.

folderPath将返回给定文件名的位置。

#3


0  

Here is in swift.
Print statement will print all available paths

这是快速的。 Print语句将打印所有可用路径

func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
print("Paths - \(paths)")
    let documentsDirectory = paths[0]
    return documentsDirectory
}

#1


30  

This will give u the paths for all files under documents directory

这将为您提供文档目录下所有文件的路径

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

    NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];

    NSLog(@"files array %@", filePathsArray);

You can get the path for each object in filePathsArray by using the below code

您可以使用以下代码获取filePathsArray中每个对象的路径

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:0]]; 

#2


0  

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

To get files from that, you have to give the path structure...

要从中获取文件,您必须提供路径结构...

    NSString *fileName = @"default.png";
    NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"%@", fileName];

The folderPath will return you the location of given file name.

folderPath将返回给定文件名的位置。

#3


0  

Here is in swift.
Print statement will print all available paths

这是快速的。 Print语句将打印所有可用路径

func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
print("Paths - \(paths)")
    let documentsDirectory = paths[0]
    return documentsDirectory
}