How do I search for a list of files using wildcard

How do I use wildcards in C# to list down files contained in a selected folder?


Directory.GetFiles is your friend here:

Directory.GetFiles(@"C:\Users\Me\Documents", "*.docx");

or, recursively:

Directory.GetFiles(
    @"C:\Users\Me\Documents",
    "*.docx",
    SearchOption.AllDirectories);

Use DirectoryInfo.GetFiles

using System.IO;

DirectoryInfo folder = new DirectoryInfo(@"C:\foo\bar");
if (folder.Exists) // else: Invalid folder!
{
    FileInfo[] files = folder.GetFiles("*.xml");

    foreach (FileInfo file in files)
    {
        DoSomething(file.FullName);
    }
}

You can do something like this:

string[] files = Directory.GetFiles(@"c:\myfolder", "*.txt", SearchOption.AllDirectories)