James Cadd James Cadd - 29 days ago 12
C# Question

C# - Get a list of files excluding those that are hidden

Directory.GetFiles()
returns all files, even those that are marked as hidden. Is there a way to get a list of files that excludes hidden files?

Answer

This should work for you:

DirectoryInfo directory = new DirectoryInfo(@"C:\temp");
FileInfo[] files = directory.GetFiles();

var filtered = files.Where(f => !f.Attributes.HasFlag(FileAttributes.Hidden));

foreach (var f in filtered)
{
    Debug.WriteLine(f);
}