檢查檔案權限
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) 權限,對照下來與程式得到的結果是相符的。