Ali Vojdanian
Ali Vojdanian

Reputation: 2111

hidden directory in C#

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

Upvotes: 0

Views: 250

Answers (2)

Mickael V.
Mickael V.

Reputation: 1242

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

dir.Attributes.HasFlag(FileAttributes.Hidden)

Upvotes: 2

rory.ap
rory.ap

Reputation: 35338

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.

Upvotes: 2

Related Questions