I have this loop:
foreach (DirectoryInfo dir in downloadedMessageInfo.GetDirectories())
{
if (dir.Attributes != FileAttributes.Hidden)
Attributes is a Flags value, so you need to check if it contains FileAttributes.Hidden using a bitwise comparison, like this:
if ((dir.Attributes & FileAttributes.Hidden) == 0)
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.
This code works for me in VB.Net;
If (dir.Attributes.Tostring.Contains("Hidden") Then
' File is hidden
Else
' File is not hidden
EndIf
In .NET 4.0 you can do:
dir.Attributes.HasFlag(FileAttributes.Hidden)