How to get All paths for files in Documents directory?

前端 未结 3 905
名媛妹妹
名媛妹妹 2020-12-13 21:41

I usually get paths by using NSBundle.

But, NSBundle does not contain Documents folder.

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

相关标签:
3条回答
  • 2020-12-13 21:59

    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

    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:0]]; 
    
    0 讨论(0)
  • 2020-12-13 22:20
        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.

    0 讨论(0)
  • 2020-12-13 22:22

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

    func getDocumentsDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    print("Paths - \(paths)")
        let documentsDirectory = paths[0]
        return documentsDirectory
    }
    
    0 讨论(0)
提交回复
热议问题