如何测试,如果目录是隐藏在C#? [英] How to test if directory is hidden in C#?

查看:180
本文介绍了如何测试,如果目录是隐藏在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的循环:

  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.

这篇关于如何测试,如果目录是隐藏在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆