I am trying to use the Directory.GetFiles()
method to retrieve a list of files of multiple types, such as mp3
's and jpg
's. I have tried both of the following with no luck:
Directory.GetFiles("C:\\path", "*.mp3|*.jpg", SearchOption.AllDirectories);
Directory.GetFiles("C:\\path", "*.mp3;*.jpg", SearchOption.AllDirectories);
Is there a way to do this in one call?
What about
Nop... I believe you have to make as many calls as the file types you want.
I would create a function myself taking an array on strings with the extensions I need and then iterate on that array making all the necessary calls. That function would return a generic list of the files matching the extensions I'd sent.
Hope it helps.
for
You could:
Directory.EnumerateFiles
for a performance boost (What is the difference between Directory.EnumerateFiles vs Directory.GetFiles?).EndsWith("aspx", StringComparison.OrdinalIgnoreCase)
rather than.ToLower().EndsWith("aspx")
)But the real benefit of
EnumerateFiles
shows up when you split up the filters and merge the results:It gets a bit faster if you don't have to turn them into globs (i.e.
exts = new[] {"*.mp3", "*.jpg"}
already).Performance evaluation based on the following LinqPad test (note:
Perf
just repeats the delegate 10000 times) https://gist.github.com/zaus/7454021( reposted and extended from 'duplicate' since that question specifically requested no LINQ: Multiple file-extensions searchPattern for System.IO.Directory.GetFiles )
The following function searches on multiple patterns, separated by commas. You can also specify an exclusion, eg: "!web.config" will search for all files and exclude "web.config". Patterns can be mixed.
Usage:
in .NET 2.0 (no Linq):
Then use it:
i don t know what solution is better, but i use this: