[英]How to test if directory is hidden in C#?
I have this loop: 我有这个循环:
foreach (DirectoryInfo dir in downloadedMessageInfo.GetDirectories())
{
if (dir.Attributes != FileAttributes.Hidden)
{
dir.Delete(true);
}
}
How can I correctly skip all hidden directories? 如何正确跳过所有隐藏目录?
Change your if statement to: 将您的if语句更改为:
if ((dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
You need to use the bitmask since Attributes is a flag enum. 您需要使用位掩码,因为Attributes是一个标志枚举。 It can have multiple values, so hidden folders may be hidden AND another flag. 它可以有多个值,因此可以隐藏隐藏文件夹和另一个标志。 The above syntax will check for this correctly. 上面的语法将正确检查。
在.NET 4.0中,您可以:
dir.Attributes.HasFlag(FileAttributes.Hidden)
Attributes
是Flags
值,因此您需要使用按位比较来检查它是否包含FileAttributes.Hidden
,如下所示:
if ((dir.Attributes & FileAttributes.Hidden) == 0)
This code works for me in VB.Net; 这段代码在VB.Net中适用于我;
If (dir.Attributes.Tostring.Contains("Hidden") Then
' File is hidden
Else
' File is not hidden
EndIf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.