How do I search for multiple file extensions? (I can have it find one).

Hey guys, I am using this line of code to search for certain files, but I want it to find multiple files.

var fileInfo = info.GetFiles("*.jpg*"); //This searches for jpegs fine.

var fileInfo = info.GetFiles("*.jpg*" && "*.tga*"); //How do I use something like this?

Cheers.

Try creating an extension method? That’s more readable.

public static IEnumerable<FileInfo> GetFilesByExtensions(this DirectoryInfo dir, params string[] extensions)
{
    if (extensions == null) 
         throw new ArgumentNullException("extensions");
    IEnumerable<FileInfo> files = dir.EnumerateFiles();
    return files.Where(f => extensions.Contains(f.Extension));
}

Usage:

dInfo.GetFilesByExtensions(".jpg",".exe",".gif");