0

I made a hidden directory in drive F and named it "File".

This code shows it is hidden file : Console.WriteLine(dc.Attributes);

But when I use DirectoryInfo Attributes to check if it's a hidden file it won't work.

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. What should I do with that?

Thanks in advance

Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47

2 Answers2

2

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:

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

I would suggest you read about how bitwise combinations work.

rory.ap
  • 34,009
  • 10
  • 83
  • 174
2

If you're using .NET 4 and above do :

dir.Attributes.HasFlag(FileAttributes.Hidden)
Mickael V.
  • 1,109
  • 11
  • 21