如何测试,如果目录是隐藏在C#?测试、目录

2023-09-02 23:50:26 作者:你被夸父追着日

我有这样的循环:

  foreach (DirectoryInfo dir in downloadedMessageInfo.GetDirectories())
        {
            if (dir.Attributes != FileAttributes.Hidden)
            {
                dir.Delete(true);
            }
        }

我怎样才能正确地跳过所有隐藏的目录?

How can I correctly skip all hidden directories?

推荐答案

更​​改您的if语句:

Change your if statement to:

if ((dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)

您需要使用掩码,因为属性是一个标志枚举。它可以有多个值,让隐藏的文件夹可能被隐藏,另一个标志。上述语法将检查这个正确。

You need to use the bitmask since Attributes is a flag enum. It can have multiple values, so hidden folders may be hidden AND another flag. The above syntax will check for this correctly.