Make DirectoryInfo.GetFiles Method Ignore Hidden Files/Folders

Is there an easy way to make DirectoryInfo.GetFiles Method Ignore Hidden Files/Folders?
Ray TurnerSenior ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Ray

Don't know if it's an easy way but you could use LINQ to filter out the hidden files/folders.
Dim di As DirectoryInfo = New DirectoryInfo("C:\test\")
Dim allfl() As FileInfo

        allfl = di.GetFiles()

        Dim visfl = From f In allfl Where f.Attributes.HasFlag(FileAttributes.Hidden) = False

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
Norie, is spot on, neither GetFiles nor EnumerateFiles offer a method which will exclude files of specific attributes (you can exclude/include files using a search filter but that matches based on name/extension).  In order to filter by attribute you will have to filter the resulting set of files returned from the methods.  Using GetFiles will block until all files are returned while using EnumerateFiles will return an Enumerated collection.

Example implementation:
Imports System.IO

Module Module1
    Sub Main()
        Dim files = GetFiles("C:\_admin", FileAttribute.Hidden)
        Dim files2 = New DirectoryInfo("C:\_admin").EnumerateFiles().Where(Function(file) (file.Attributes And FileAttribute.Hidden) = 0)

        For Each file In files
            Console.WriteLine(file)
        Next

        For Each file In files2
            Console.WriteLine(file)
        Next

        Console.ReadLine()
    End Sub

    Iterator Function GetFiles([directory] As String, exclude As FileAttribute) As IEnumerable(Of FileInfo)
        For Each file In New DirectoryInfo([directory]).GetFiles()
            If (file.Attributes And exclude) = 0 Then
                Yield file
            End If
        Next
    End Function
End Module

Open in new window

For the following folder:Capture.PNGProduces the following output:Capture.PNGChanging the second call to only include hidden files:
Imports System.IO

Module Module1
    Sub Main()
        Dim files = GetFiles("C:\_admin", FileAttribute.Hidden)
        Dim files2 = New DirectoryInfo("C:\_admin").EnumerateFiles().Where(Function(file) (file.Attributes And FileAttribute.Hidden) = FileAttribute.Hidden)

        For Each file In files
            Console.WriteLine(file)
        Next

        For Each file In files2
            Console.WriteLine(file)
        Next

        Console.ReadLine()
    End Sub

    Iterator Function GetFiles([directory] As String, exclude As FileAttribute) As IEnumerable(Of FileInfo)
        For Each file In New DirectoryInfo([directory]).GetFiles()
            If (file.Attributes And exclude) = 0 Then
                Yield file
            End If
        Next
    End Function
End Module

Open in new window

Produces the following output -Capture.PNG
-saige-
0
Ray TurnerSenior ConsultantAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.