[Solved] c# Multiple filters or File extensions with Directory.GetFiles()

Hi Guys,

So I had this problem where I wanted to get Files with extensions .js and .css.


As you know that Directory.GetFiles() does not support multiple filters but there are many ways you can get it to work.

Whatever you do just be aware that processing a large set of files under a directory and sub directories has memory and performance issues associated with it.

Below is a faster way to search files that are returned by Directory.GetFiles() with SearchOption set to AllDirectories

Pre-requisite for this method is LINQ

First I declare an array with extensions that I want my GetFiles method to absorb and return all files that ends with extensions provided in the below array.

[geshi lang=”csharp” nums=”1″ target=”_self” ]

Now I declare a static method called GetFiles definition is shown below

[geshi lang=”csharp” nums=”1″ target=”_self” ]


Now I will use the above function as shown below

[geshi lang=”csharp” nums=”1″ target=”_self” ]

The above will print all files that ends with .sln and .suo as shown below

Just as a side note. Above is what I use all the time. But this is not the only method and this method will require you to use LINQ. With Older .NET framework where there is not support for LINQ this method will not work. So I rely on the method given below

I Hope this helps


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.