检查文件存在于iPhone目录内的文件夹中

时间:2012-08-01 07:18:42

标签: iphone ipad nsbundle file-exists nsdocumentdirectory

我是iPhone新手,

我想检查Myfile内的文件夹中是否存在DocumentDirectory

例如:

Myfile.epub是我的档案之一,我想检查此档案是否存在于我的DestPath

DestPath是我的DocumentDirectory路径。

DestPath=/Users/krunal/Library/Application Support/iPhone Simulator/5.0/Applications/D414BC19-C005-4D93-896D-A6FB71DE4D21/Documents/Derivatives

我们将不胜感激。

3 个答案:

答案 0 :(得分:3)

这个解决方案对我有用..
 您不希望提供路径的整个部分,如 / Users / krunal / Library / Application Support / iPhone Simulator / 5.0 / Applications / D414BC19-C005-4D93-896D-A6FB71DE4D21 / Documents / Derivatives

你必须提供 Derivatives / Myfile.epub(FolderName / filename)来检查文件路径

    NSFileManager *filemanager=[NSFileManager defaultManager];  

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

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Derivatives/Myfile.epub"];

    BOOL success =[filemanager fileExistsAtPath:path];

    if (success == YES) {   

        NSLog(@"exists");
    }
    else {

       NSLog(@"not exists");

    }

答案 1 :(得分:2)

你也可以试试..

NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"file"];
success=[filemanager fileExistsAtPath:path];

答案 2 :(得分:0)

 //Get the Document directory path 

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
 NSString *documentsPath = [paths objectAtIndex:0]; 
 documentsPath = [documentsPath stringByAppendingPathComponent:@"Myfile.epub"];
 if (![[NSFileManager defaultManager] fileExistsAtPath:documentsPath]) 
 {
    //file not exits in Document Directories
 }
  else
  {
    //file exist in Document Directories
  }