【问题标题】:Check file exists in a folder inside Directory in iPhone检查文件是否存在于 iPhone 目录中的文件夹中
【发布时间】:2012-07-30 01:25:27
【问题描述】:

我是 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

我们将不胜感激。

【问题讨论】:

标签: iphone ipad nsbundle file-exists nsdocumentdirectory


【解决方案1】:

这个解决方案对我有用..
您不想提供路径的整个部分,例如 /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");

    }

【讨论】:

  • 检查我的DestPath 文件夹中有一个文件夹我想检查Myfile.epub 是否存在于DestPath 中。
  • 您必须提供 FolderName/Myfile.epub 以检查文件路径
【解决方案2】:

你也可以试试..

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

【讨论】:

  • 检查我的DestPath 里面有一个文件夹我想检查Myfile.epub 是否存在于DestPath 里面。
【解决方案3】:
 //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
  }

【讨论】:

    猜你喜欢
    • 2016-05-20
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 2012-06-28
    • 2011-03-16
    • 1970-01-01
    • 2012-02-05
    • 2022-11-01
    相关资源
    最近更新 更多