NSString *homeDirectory = NSHomeDirectory(); NSString *fileDirectory = [homeDirectory stringByAppendingPathComponent:@"temp/app_data.plist"]; //使用NSSearchPathForDirectoriesInDomains检索指定路径 NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //常量NSDocumentDirectory表示正在查找Documents目录的路径(使用NSCachesDirectory表明要查找的时Caches文件夹),常量NSUserDomainMask表明我们希望将搜索限制于我们应用程序的沙盒,最后一个参数决定了是否“展开”波浪线符号。 //在Mac系统中,‘~’表示主路经(Home),如果不展开,路径看起来就是:‘~/Documents’,展开后即得到完整路径。这个参数一直设置位真即可。 NSString *documentsDirectory = [paths objectAtIndex:0];z NSString *fileDirectory = [documentsDirectory stringByAppendingPathComponent:@"file.txt"]; //使用Foundation中的NSTemporaryDirectory函数直接返回代表temp文件夹的全路径的字符串对象 NSString *tempDirectory = NSTemporaryDirectory(); NSString *file = [tempDirectory stringByAppendingPathComponent:@"file.txt"]; Example: NSArray *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *docDir = [path objectAtIndex:0]; NSLog(@"filepath:%@",docDir); NSString *str = @"hello.jpg"; NSString *filepath = [docDir stringByAppendingPathComponent:str]; //NSString *filepath = [docDir stringByAppendingPathComponent:[NSString stringWithUTF8String:"///mest.txt"]]; NSLog(@"filepath:%@",filepath); BOOL success = [[NSFileManager defaultManager]createFileAtPath: filepath contents:nil attributes:nil]; NSLog(@"result",success); printf("Create File:%s %s.",[filepath UTF8String], success ? "Success" : "Error"); NSString* reValue= [NSString stringWithString:@"\"success\""];
通过NSHomeDirectory获得文件路径
最新推荐文章于 2022-03-18 17:21:00 发布