How to test if directory is hidden in C#?

前端 未结 4 1975

I have this loop:

  foreach (DirectoryInfo dir in downloadedMessageInfo.GetDirectories())
        {
            if (dir.Attributes != FileAttributes.Hidden)
         


        
相关标签:
4条回答
  • 2020-12-15 04:56

    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)
    
    0 讨论(0)
  • 2020-12-15 05:01

    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.

    0 讨论(0)
  • 2020-12-15 05:03

    This code works for me in VB.Net;

    If (dir.Attributes.Tostring.Contains("Hidden") Then
        ' File is hidden
    Else
        ' File is not hidden
    EndIf
    
    0 讨论(0)
  • 2020-12-15 05:06

    In .NET 4.0 you can do:

    dir.Attributes.HasFlag(FileAttributes.Hidden)
    
    0 讨论(0)
提交回复
热议问题