Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DirectoryInfo.GetFiles, How to get different types of files in C#

How can I find both file types *.gif and *.jpg using DirectoryInfo.GetFiles function in C# ?

When I try this code:

string pattern = "*.gif|*.jpg";
FileInfo[] files = dir.GetFiles(pattern);

The exception "Illegal characters in path." is thrown.

like image 682
ihorko Avatar asked Feb 19 '13 15:02

ihorko


1 Answers

THere is no way to combine into one string pattern, you have to store patterns in a list:

var extensions = new[] { "*.gif", "*.jpg" };
var files = extensions.SelectMany(ext => dir.GetFiles(ext));
like image 145
cuongle Avatar answered Sep 30 '22 20:09

cuongle