4

Possible Duplicate:
GetFiles with multiple extentions

is there a function like GetFiles that takes more then 1 file type like

DirectoryInfo di = new DirectoryInfo("c:/inetpub/wwwroot/demos");
FileInfo[] rgFiles = di.GetFiles("*.bmp, *.jpg, etc");
Community
  • 1
  • 1
Kimtho6
  • 6,154
  • 9
  • 40
  • 56
  • 2
    As well as solving the immediate problem, you also need to bear in mind that a file's extension isn't a guarantee of its content. – AakashM Nov 08 '10 at 13:41

5 Answers5

9

AFAIK, this isn't directly possible.

Instead, you can get every file, then filter the array:

HashSet<string> allowedExtensions = new HashSet<string>(extensionArray, StringComparer.OrdinalIgnoreCase);
FileInfo[] files = Array.FindAll(dirInfo.GetFiles(), f => allowedExtensions.Contains(f.Extension));

extensionArray must include . before each extension, but is case-insensitive.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
5

Not that I know of. I implemented the same problem like so:

DirectoryInfo di = new DirectoryInfo("c:/inetpub/wwwroot/demos");
FileInfo[] rgFiles = di.GetFiles("*.bmp")
    .Union(di.GetFiles("*.jpg"))
    .Union(di.GetFiles("etc"))
    .ToArray();

Note that this requires the System.Linq namespace.

Codesleuth
  • 10,321
  • 8
  • 51
  • 71
  • @Codesleuth are you sure it's more efficient to query the folder 3 times compared to SLacks approach which is gathering the filenams only once – fubo Apr 26 '16 at 09:05
  • I haven't gone to any length to compare the efficiency of this, but I can hazard a guess that listing the entire contents of a directory into memory then filtering is in the majority of cases more efficient than requesting three separate filtered lists from the file system. Personally, I would use SLaks' approach. – Codesleuth Apr 26 '16 at 18:23
0

How to get files with multiple extensions

Community
  • 1
  • 1
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
0

If you want your code to be bullet proof in the sense your file detection mechanism detects an image file not based on the extension but on the nature of the file, you'd have to to load your files as byte[] and look for a magic trail of bytes usually in the beginning of the array. Every graphical file has it's own way of manifesting itself to the software through presenting that magic value of bytes. I can post some code examples if you'd like.

dexter
  • 7,063
  • 9
  • 54
  • 71
0

No, theres not. Windows does not have a way to separate filters in the search pattern. This could be done manually through LINQ, though.

By using the EnumerateFiles you'll get results as they come back so you don't have to wait for all the files in order to start working on the result.

        var directory = new DirectoryInfo("C:\\");
        var allowedExtensions = new string[] { ".jpg", ".bmp" };

        var imageFiles = from file in directory.EnumerateFiles("*", SearchOption.AllDirectories)
                         where allowedExtensions.Contains(file.Extension.ToLower())
                         select file;

        foreach (var file in imageFiles)
            Console.WriteLine(file.FullName);
Noring
  • 1