需要访问ios中的目录列表

时间:2015-08-31 09:33:18

标签: ios objective-c iphone swift

我需要在此路径中创建一个文件夹和文件 /var/mobile/Containers/Data/ 但是当我找它时找不到文件夹 我的代码>>

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = @"/var/mobile/Containers";//[paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyNewFolder/myDB.db"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

4 个答案:

答案 0 :(得分:0)

我真的明白你的问题。希望这会有所帮助:

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

        NSLog(@"Saving");

        NSUserDefaults *prefs = [[NSUserDefaults alloc] init];

        NSString *path1 = [NSString stringWithFormat:@"/var/mobile/Containers"];

        NSString *dataPath = [path stringByAppendingPathComponent:path1];

        dataPath = [dataPath stringByStandardizingPath];

                filePath = [dataPath stringByAppendingPathComponent:@"MyNewFolder/myDB.db"];

答案 1 :(得分:0)

这是iOS 8方法Apple recommends to retrieve the location of the Documents directory

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

答案 2 :(得分:0)

  

创建文件夹

  //-- to select path from image--//
    AppDelegate *del = (AppDelegate*)[[UIApplication sharedApplication]delegate];
    NSString *dataPath=[del.databasePath copy];
    NSString *Documentpath=[del.path_Folder_Document copy];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory
    dataPath = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:@"/Images"];
    Pathforimage=dataPath;
  

如果不存在则添加文件夹

 if (![[NSFileManager defaultManager] createDirectoryAtPath:Pathforimage withIntermediateDirectories:NO attributes:nil error:nil])
    {
        //NSLog(@"directory... /Images already exist..");
    }
    else
    {
        //NSLog(@"directory created!");
    }
    NSString *pathforAudios=[NSString stringWithFormat:@"%@/Images_Audio",Documentpath];
    if (![[NSFileManager defaultManager] createDirectoryAtPath:pathforAudios withIntermediateDirectories:NO attributes:nil error:nil])
    {
        //NSLog(@"directory... /Images already exist..");
    }
    else
    {
        //NSLog(@"directory created!");
    }

    //-- end of  path --//
  

将图像保存到文件夹

   //Save Image to Documents Directory
   NSString  *imagePath = [Pathforimage stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",Img_File_Name]];
   [UIImageJPEGRepresentation(image, 0.1) writeToFile:imagePath atomically:YES];
   imagePath=nil;
   image=nil;
  

删除文件/文件夹

  NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];
    NSLog(@"files =%@",files);

     NSString *Images_Folder = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:@"/Images"];
NSArray *fileArray = [fileMgr contentsOfDirectoryAtPath:Images_Folder error:nil];
for (NSString *filename in fileArray)
{
    NSLog(@"Images =%@",filename);
    [fileMgr removeItemAtPath:[Images_Folder stringByAppendingPathComponent:filename] error:NULL];
}

NSString *Images_Audio_Folder = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:@"/Images_Audio"];
NSArray *fileArray1 = [fileMgr contentsOfDirectoryAtPath:Images_Audio_Folder error:nil];
for (NSString *filename in fileArray1)
{
    NSLog(@"Images_Audio =%@",filename);
    [fileMgr removeItemAtPath:[Images_Audio_Folder stringByAppendingPathComponent:filename] error:NULL];
}

答案 3 :(得分:0)

创建floder

func playbackSession(session: AnyObject<BCOVPlayBackSession>!, didReceiveLifecycleEvent event:BCOVPlaybackSessionLifecycleEvent!)