15 Haz

C# Get Hidden Files In A Directory

This example list all hidden files on the specified path.

Code Sample:

            //Suppose we have this sample directory and files

            //C:\TestDirectory\Files\file1.txt
            //C:\TestDirectory\Files\file2.txt //Hidden
            //C:\TestDirectory\Files\file3.txt
            //C:\TestDirectory\Files\file4.txt //Hidden
            //C:\TestDirectory\Files\file5.txt

            string[] filePaths = Directory.EnumerateFiles(@"C:\TestDirectory\Files\").
                                           Where(f => (new FileInfo(f).Attributes & FileAttributes.Hidden) == FileAttributes.Hidden).
                                           ToArray();
            foreach (string file in filePaths)
                Console.WriteLine(file);
            //Output:
            //C:\TestDirectory\Files\file2.txt
            //C:\TestDirectory\Files\file4.txt