I am trying to use the Directory.GetFiles()
method to retrieve a list of files of multiple types, such as mp3
's and jpg
's. I have tried both of the following with no luck:
Directory.GetFiles("C:\\path", "*.mp3|*.jpg", SearchOption.AllDirectories);
Directory.GetFiles("C:\\path", "*.mp3;*.jpg", SearchOption.AllDirectories);
Is there a way to do this in one call?
Make the extensions you want one string i.e ".mp3.jpg.wma.wmf" and then check if each file contains the extension you want. This works with .net 2.0 as it does not use LINQ.
The advantage with this approach is you can add or remove extensions without editing the code i.e to add png images, just write myExtensions=".jpg.mp3.png".
I had the same problem and couldn't find the right solution so I wrote a function called GetFiles:
This function will call
Directory.Getfiles()
only one time.For example call the function like this:
EDIT: To get one file with multiple extensions use this one:
For example call the function like this: