简体   繁体   English

C#中的隐藏目录

[英]hidden directory in C#

I made a hidden directory in drive F and named it "File". 我在驱动器F中创建了一个隐藏目录,并将其命名为“文件”。

This code shows it is hidden file : Console.WriteLine(dc.Attributes); 此代码显示它是隐藏文件: Console.WriteLine(dc.Attributes);

But when I use DirectoryInfo Attributes to check if it's a hidden file it won't work. 但是,当我使用DirectoryInfo Attributes检查它是否为隐藏文件时,它将无法工作。

Here is the code : 这是代码:

DirectoryInfo dc = new DirectoryInfo(@"F:\File");
        Console.WriteLine(dc.Attributes);
        if (dc.Attributes == FileAttributes.Hidden)
        {
            Console.WriteLine("HIDDEN");
        }
        else
        {
            Console.WriteLine("NOT HIDDEN");
        }

It writes NOT HIDDEN . 它写NOT HIDDEN What should I do with that? 我该怎么办?

Thanks in advance 提前致谢

The problem is that the attributes value is a bitwise combination of multiple attributes. 问题在于属性值是多个属性的按位组合。

To test whether the FileAttributes.Hidden attribute is set, you need to do this: 要测试是否设置了FileAttributes.Hidden属性,您需要执行以下操作:

if ((dc.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)

I would suggest you read about how bitwise combinations work . 我建议您阅读一下按位组合的工作原理

如果您使用的是.NET 4及更高版本,请执行以下操作:

dir.Attributes.HasFlag(FileAttributes.Hidden)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM