Doesn't the Directory.GetFiles(String, String)
overload already do that? You would just do Directory.GetFiles(dir, "*.jpg", SearchOption.AllDirectories)
If you want to put them in a list, then just replace the "*.jpg"
with a variable that iterates over a list and aggregate the results into an overall result set. Much clearer than individually specifying them. =)
Something like...
foreach(String fileExtension in extensionList){
foreach(String file in Directory.GetFiles(dir, fileExtension, SearchOption.AllDirectories)){
allFiles.Add(file);
}
}
(If your directories are large, using EnumerateFiles
instead of GetFiles
can potentially be more efficient)