C#
File Search
System.IO
Directory.GetFiles
Programming

Directory.GetFiles search pattern with multiple extensions in C#

Codemia Knowledge Hub ·

Directory.GetFiles accepts one wildcard search pattern such as *.txt. It does not understand a compound pattern like *.txt|*.csv. For multiple extensions, enumerate once and filter by extension in code, or run one search per extension and combine the results.

What searchPattern supports

The searchPattern argument does wildcard matching, not a query language. These patterns work:

  • *.txt
  • report-*.csv
  • file?.log

This does not work:

csharp
Directory.GetFiles(path, "*.txt|*.csv");

The pipe has no special meaning to GetFiles, and neither do ; or ,. The method treats the string as a literal pattern and will not return what you intended.

Enumerate once and filter by extension

Use EnumerateFiles and filter with Path.GetExtension. Results stream instead of being allocated up front, which matters for large directories, and the logic stays readable.

csharp
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5
6class Program
7{
8    static IEnumerable<string> FindFiles(string path, params string[] extensions)
9    {
10        var allowed = new HashSet<string>(
11            extensions.Select(ext => ext.StartsWith(".") ? ext : "." + ext),
12            StringComparer.OrdinalIgnoreCase);
13
14        return Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)
15            .Where(file => allowed.Contains(Path.GetExtension(file)));
16    }
17
18    static void Main()
19    {
20        foreach (string file in FindFiles(@"C:\Docs", ".txt", ".csv"))
21        {
22            Console.WriteLine(file);
23        }
24    }
25}

StringComparer.OrdinalIgnoreCase handles case-insensitive matching, which is easy to forget on Windows when you compare extensions manually. This approach is especially useful when you have many extensions.

Separate calls for a few extensions

If the directory is small and there are only a couple of extensions, separate calls are straightforward:

csharp
1using System;
2using System.IO;
3using System.Linq;
4
5class Program
6{
7    static void Main()
8    {
9        string path = @"C:\Docs";
10
11        string[] files = Directory.GetFiles(path, "*.txt")
12            .Concat(Directory.GetFiles(path, "*.csv"))
13            .ToArray();
14
15        foreach (string file in files)
16        {
17            Console.WriteLine(file);
18        }
19    }
20}

The directory gets scanned once per call. On local disks that may be acceptable; on network shares or deep recursive searches it is less attractive.

Searching subdirectories

The same filtering works recursively:

csharp
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5
6class Program
7{
8    static IEnumerable<string> FindFilesRecursive(string path, params string[] extensions)
9    {
10        var allowed = new HashSet<string>(extensions, StringComparer.OrdinalIgnoreCase);
11
12        return Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
13            .Where(file => allowed.Contains(Path.GetExtension(file)));
14    }
15
16    static void Main()
17    {
18        foreach (string file in FindFilesRecursive(@"C:\Projects", ".cs", ".xaml"))
19        {
20            Console.WriteLine(file);
21        }
22    }
23}

With SearchOption.AllDirectories, expect permission errors in protected folders. A recursive search does not quietly skip inaccessible directories; it can throw depending on the path and permissions. Write custom traversal logic if you want to skip them instead of failing the whole search.

Wrapping it in a helper method

If multi-extension lookup appears in more than one place, put it behind a helper method instead of repeating LINQ filters. That keeps extension normalization, case-insensitive comparison and traversal options consistent, and later changes such as excluding temporary folders or adding cancellation happen in one place.

Regex is rarely needed

You can apply a regular expression after enumeration, but for extension checks Path.GetExtension is clearer and cheaper. Use a regex only when the naming rule is more complex than an extension match.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms