C#: Recursively getting a list of files with a specific extension

leave a comment »

How to recursively get a list of files with a specific extension in C#.


sDir – Starting directory
SExtension – file extension to filter by
List files – A empty list that is used to keep found files while recursing.

        public List<string> getFiles(string sDir, string sExtension, List<string> files)
            foreach (string d in Directory.GetDirectories(sDir))
                foreach (string f in Directory.GetFiles(d))
                    if (f.EndsWith(sExtension))
                getFiles(d, sExtension, files);
            return files;

Starting Source:

Written by M Kapoor

September 22, 2014 at 3:28 pm

Posted in c#

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: