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");
E.g. filess = FileExtentio + ", " + FileExtentio2 + ", " + ... or filess = string. Format("{0}, {1}, ...", FileExtentio, FileExtentio2, ...) or filess = $"{FileExtentio}, {FileExtentio2}, ..."
A file name may have no extensions. Sometimes it is said to have more than one extension, although terminology varies in this regard, and most authors define extension in a way that doesn't allow more than one in the same file name. More than one extension usually represents nested transformations, such as files. tar.
Find files with multiple file extensions jpg , . gif ). 2.2 The isEndWith() method can be shorter with Java 8 stream anyMatch . 2.3 We also can remove the isEndWith() method and puts the anyMatch into the filter directly.
Why not create 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 = Enumerable.Empty<FileInfo>(); foreach(string ext in extensions) { files = files.Concat(dir.GetFiles(ext)); } return files; }
EDIT: a more efficient version:
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:
DirectoryInfo dInfo = new DirectoryInfo(@"c:\MyDir"); dInfo.GetFilesByExtensions(".jpg",".exe",".gif");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With