Possible Duplicate:
Can you call Directory.GetFiles() with multiple filters?
How do you filter on more than one extension?
I've tried:
FileInfo[] Files = dinfo.GetFiles("*.jpg;*.tiff;*.bmp");
FileInfo[] Files = dinfo.GetFiles("*.jpg,*.tiff,*.bmp");
You can get every file, then filter the array:
This will be (marginally) faster than every other answer here.
In .Net 3.5, replace
EnumerateFiles
withGetFiles
(which is slower).And use it like this:
Why not create an extension method? That's more readable.
EDIT: a more efficient version:
Usage:
You can't do that, because
GetFiles
only accepts a single search pattern. Instead, you can callGetFiles
with no pattern, and filter the results in code:If you're working with .NET 4, you can use the
EnumerateFiles
method to avoid loading all FileInfo objects in memory at once:I'm not sure if that is possible. The MSDN GetFiles reference says a search pattern, not a list of search patterns.
I might be inclined to fetch each list separately and "foreach" them into a final list.
I know there is a more elegant way to do this and I'm open to suggestions... this is what I did:
The following retrieves the jpg, tiff and bmp files and gives you an
IEnumerable<FileInfo>
over which you can iterate:If you really need an array, simply stick
.ToArray()
at the end of this.