15 Jun

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

2 thoughts on “C# Get Hidden Files In A Directory

  1. Hello thanks for sharing the information.

    I am a newbie computer programming student and have a lab work. Whatever I need to move a folder with all the sub folders and files to a target path. I could not accomplish, can you help me.

    Thanks in advance.

  2. You can use “Move” method in System.IO.Directory class.

    Sample usage:
    Directory.Move(@”C:\SourceDirectory”, @”C:\DestinationDirectory”);

Leave a Reply to Clark Darko Cancel reply

Your email address will not be published. Required fields are marked *