Find Hidden Files in a Directory in C#

Whether a file is hidden is stored in its attributes. To list hidden files, enumerate the folder and keep the entries whose Attributes include FileAttributes.Hidden:

// 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

var hiddenFiles = new DirectoryInfo(@"C:\TestDirectory\Files")
    .EnumerateFiles()
    .Where(f => f.Attributes.HasFlag(FileAttributes.Hidden));

foreach (FileInfo file in hiddenFiles)
{
    Console.WriteLine(file.FullName);
}
// C:\TestDirectory\Files\file2.txt
// C:\TestDirectory\Files\file4.txt

DirectoryInfo.EnumerateFiles is the better starting point than Directory.EnumerateFiles here. It returns FileInfo objects whose attributes come from the same directory read that produced the names. The older version of this example called new FileInfo(path).Attributes for each path, which asks the file system again for every file.

HasFlag used to be slow because it boxed the enum; since .NET Core 2.1 the JIT compiles it to the same bit test as (f.Attributes & FileAttributes.Hidden) != 0, so use whichever reads better to you.

Include subfolders

To search a whole tree, pass EnumerationOptions. There is a catch:

var options = new EnumerationOptions
{
    RecurseSubdirectories = true,
    IgnoreInaccessible = true,
    AttributesToSkip = 0 // the default skips hidden and system files
};

var hiddenFiles = new DirectoryInfo(@"C:\TestDirectory\Files")
    .EnumerateFiles("*", options)
    .Where(f => f.Attributes.HasFlag(FileAttributes.Hidden));

A new EnumerationOptions has AttributesToSkip set to FileAttributes.Hidden | FileAttributes.System. Forget to clear it and the query above returns nothing, without any error. The overloads that take a SearchOption instead of EnumerationOptions do not skip anything, so EnumerateFiles("*", SearchOption.AllDirectories) also works, but it throws on the first folder you cannot read.

The same default is handy when you want the opposite: pass new EnumerationOptions() without touching AttributesToSkip and you get only files that are neither hidden nor system files.

Hidden directories work the same way through EnumerateDirectories, which returns DirectoryInfo objects with the same Attributes property. Keep in mind that files inside a hidden folder are not hidden themselves unless they have the attribute too.

A single file

To check one path, read its attributes directly:

bool isHidden = File.GetAttributes(@"C:\TestDirectory\Files\file2.txt")
    .HasFlag(FileAttributes.Hidden);

To hide or unhide a file on Windows, add or remove the flag and write the attributes back:

string path = @"C:\TestDirectory\Files\file1.txt";

FileAttributes attributes = File.GetAttributes(path);
File.SetAttributes(path, attributes | FileAttributes.Hidden);   // hide
File.SetAttributes(path, attributes & ~FileAttributes.Hidden);  // unhide

Always combine with the existing attributes. File.SetAttributes(path, FileAttributes.Hidden) would also clear ReadOnly, Archive and anything else that was set.

Linux and macOS

Unix file systems have no hidden attribute. By convention, a file whose name starts with a dot is hidden, and .NET reports FileAttributes.Hidden for those files, so the code above finds .gitignore and .env on Linux without changes. The reverse does not hold: to hide a file on Linux you rename it with a leading dot, because setting the attribute does not change the name.

Related examples

2 thoughts on “Find Hidden Files in a Directory in C#”

  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.

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

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

    Reply

Leave a Comment