问题描述
如何确定某个路径是否指向隐藏文件/文件夹?
NSString * file = @ my / file / some.where;
BOOL fileIsHidden = //< - 我该怎么办?
我知道隐藏文件以句点为前缀。这不是要隐藏文件的唯一标准。我读过的地方有一个.hidden文件,还配置什么文件被隐藏。
有没有Cocoa / Carbon的方法很容易找到这一点,而不需要重写所有这些逻辑和从各种来源收集信息?
编辑:kLSItemInfoIsInvisible检查似乎适用于一些文件。它似乎不隐藏:
/ dev
/ etc
/ tmp
/ var
默认情况下,这些都被Finder隐藏。
解决方案正如海报指出,它似乎不工作在/ etc和/ var和什么没有,所以我修改了方法。
现在它采用一个isFile布尔,YES表示它的一个文件,NO表示一个目录
BOOL isInvisible(NSString * str, BOOL isFile){
CFURLRef inURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,(CFStringRef)str,kCFURLPOSIXPathStyle,isFile);
LSItemInfoRecord itemInfo;
LSCopyItemInfoForURL(inURL,kLSRequestAllFlags,& itemInfo);
BOOL isInvisible = itemInfo.flags& kLSItemInfoIsInvisible;
return(isInvisible!= 0);
}
int main(){
NSLog(@%d,isInvisible(@/ etc,NO) // => 1
NSLog(@%d,isInvisible(@/ Users,NO)); // => 0
NSLog(@%d,isInvisible(@/ mach_kernel,YES)); // => 1
}
>
How can I determine whether a certain path points to a hidden file/folder?
NSString *file = @"/my/file/some.where"; BOOL fileIsHidden = // <-- what do I do here?
I know that hidden files are prefixed by a period. This is not the only criteria for a file to be hidden. I've read somewhere that there's a .hidden file that also configures what files are hidden.
Is there a Cocoa/Carbon way to find this out easily without rewriting all this logic and gathering information from various sources?
EDIT: the kLSItemInfoIsInvisible check seems to work for some files. It doesn't seem to hide:
/dev /etc /tmp /varAll these are hidden by Finder by default.
解决方案As the poster pointed out, it doesn't seem to work on /etc and /var and what not, so I modified the method.
It now takes a "isFile" boolean, YES means its a file, NO means a directory
BOOL isInvisible(NSString *str, BOOL isFile){ CFURLRef inURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)str, kCFURLPOSIXPathStyle, isFile); LSItemInfoRecord itemInfo; LSCopyItemInfoForURL(inURL, kLSRequestAllFlags, &itemInfo); BOOL isInvisible = itemInfo.flags & kLSItemInfoIsInvisible; return (isInvisible != 0); } int main(){ NSLog(@"%d",isInvisible(@"/etc",NO)); // => 1 NSLog(@"%d",isInvisible(@"/Users",NO)); // => 0 NSLog(@"%d",isInvisible(@"/mach_kernel",YES)); // => 1 }
It seems to work on everything now!
这篇关于文件是否隐藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!