Check file exists in a folder inside Directory in iPhone

余生颓废 提交于 2019-12-11 16:26:03

问题


I am new to iPhone,

I want to check whether Myfile exists in folder inside DocumentDirectory ?

For eg:

Myfile.epub is one of my file and i want to check whether this file exists at my DestPath or not ?

DestPath is my DocumentDirectory path.

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

Any help will be appreciated.


回答1:


This solution worked for me..
You don't want to give the entire part of your path like /Users/krunal/Library/Application Support/iPhone Simulator/5.0/Applications/D414BC19-C005-4D93-896D-A6FB71DE4D21/Documents/Derivatives

you have to give Derivatives/Myfile.epub (FolderName/filename) for checking the file path

    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");

    }



回答2:


you also can try..

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



回答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
  }


来源:https://stackoverflow.com/questions/11754337/check-file-exists-in-a-folder-inside-directory-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!