Multiple file-extensions searchPattern for System.IO.Directory.GetFiles
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Directory.GetFiles accepts a single search pattern such as *.txt, but it does not support a compound pattern like *.txt|*.csv. When you need multiple extensions, the practical solution is to enumerate once and filter in code, or run multiple searches and combine the results.
What searchPattern Supports
The searchPattern argument supports wildcard matching, not a mini query language. These patterns work:
- '
*.txt' - '
report-*.csv' - '
file?.log'
This does not work:
The pipe character has no special meaning to GetFiles, so the method treats the string as a literal pattern and will not return the intended result.
A Good Modern Approach
If the directory may be large, use EnumerateFiles and filter by extension. This avoids allocating the full result set up front and keeps the logic readable.
This approach is especially useful when you have many extensions or want case-insensitive matching.
Multiple Calls Can Still Be Fine
If the directory is small and the number of extensions is tiny, separate calls are straightforward:
The downside is that the directory gets scanned more than once. On local disks that may be acceptable. On network shares or deep recursive searches, it becomes less attractive.
Recursive Searches
The same filtering idea works with subdirectories too:
If you use SearchOption.AllDirectories, be ready for permission errors in protected folders. You may need custom traversal logic if you want to skip inaccessible directories instead of failing the whole search.
When a Helper Method Pays Off
If multi-extension lookup appears in more than one place, move it behind a helper method instead of repeating LINQ filters across the codebase. That keeps extension normalization, case-insensitive comparison, and traversal options consistent. It also makes later changes, such as excluding temporary folders or adding cancellation, much easier to apply in one place.
Why Not Use Regular Expressions First
You can apply a regular expression after enumeration, but it is usually unnecessary for simple extension checks. Path.GetExtension is clearer and cheaper. Reach for a regex only when the naming rule is more complex than extension matching.
Common Pitfalls
- Trying to pass multiple patterns into
GetFileswith separators like|,;, or,. The API does not parse them. - Using
GetFilesfor huge trees whenEnumerateFileswould stream results more efficiently. - Forgetting case-insensitive matching on Windows if you compare extensions manually.
- Assuming recursive search will quietly skip inaccessible directories. It can throw exceptions depending on the path and permissions.
Summary
- '
Directory.GetFilessupports one wildcard pattern per call.' - For multiple extensions, enumerate once and filter with
Path.GetExtension. - Separate
GetFilescalls are acceptable for small, simple searches. - Prefer
EnumerateFileswhen performance or memory usage matters. - Be careful with recursive searches and permission-related exceptions.
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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.