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:
*.txtreport-*.csvfile?.log
This does not work:
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.
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:
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:
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.
Related reading
- MultipleActiveResultSetsTrue or multiple connections?
- Multiply TimeSpan in .NET
- Must call EndRead in ALL cases?
- MVC4 HTTP Error 403.14 - Forbidden
- MVVM in WPF - How to alert ViewModel of changes in Model... or should I?
- My C application is returning 0xE0434352 to Windows Task Scheduler but it is not crashing
- My images are blurry Why isn't WPF's SnapsToDevicePixels working?
- MySQL C async methods doesn't work?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.