检查档案权限
NSFileManager 提供文件权限的查询的方法,分别为:
- isReadableFileAtPath
- isWritableFileAtPath
- isExecutableFileAtPath
- isDeletableFileAtPath
每个方法都需要指定完整的路径,透过此方法会返回BOOL值为YES或NO,下面范例透过档名temp.txt
来查询:
//-----------start----------- NSFileManager *filemanager; filemanager = [NSFileManager defaultManager]; NSLog(@"isExecutableFileAtPath =%@",([filemanager isExecutableFileAtPath:@"/tmp/temp.txt"] == YES? @"YES":@" NO")); NSLog(@"isReadableFileAtPath =%@",([filemanager isReadableFileAtPath:@"/tmp/temp.txt"] == YES? @"YES":@" NO")); NSLog(@"isWritableFileAtPath =%@",([filemanager isWritableFileAtPath:@"/tmp/temp.txt"] == YES? @"YES":@" NO")); NSLog(@"isDeletableFileAtPath =%@",([filemanager isDeletableFileAtPath:@"/tmp/temp.txt"] == YES? @"YES":@" NO")); //------------end------------
输出结果:
2014-02-21 17:16:55.304 test1[56893:303] isExecutableFileAtPath = NO 2014-02-21 17:16:55.306 test1[56893:303] isReadableFileAtPath =YES 2014-02-21 17:16:55.307 test1[56893:303] isWritableFileAtPath =YES 2014-02-21 17:16:55.308 test1[56893:303] isDeletableFileAtPath =YES
另外直接用命令查看一下权限:
-rw-r--r-- 1 danny wheel 0 2 21 17:01 temp.txt
从命令查询的看到权限具备 读(r)、写(w) 但没有 执行(x) 权限,对照下来与程式得到的结果是相符的。