Recursively iterate through files in a directory structure

I was playing a little with C#’s new yield keyword and I thought I’d update the code from this article with the “Whidbey” way to do. 


Now as I am sure my good friends in perf land will tell you this is likely not the most performant way to write this code, but for the application I am writing it made no difference.   From looking at the IL, it looks like the major perf disadvantage is creating the compiler-generated enumerable class that encapsulates the code in the iterator block, not a huge cost, but something to be aware of because more stuff is going on under the covers here.


Basically I am writing an application that loads all the managed DLLs installed with LH and looks through them for certain naming patterns….   The first thing I noticed is the managed DLLs in LH are all grouped under subdirectories of “%windir%\Microsoft.NET\”.  Here is the code I wanted to be able to write:

foreach (string assemblyPath in Helpers.GetFiles(path))


   Assembly assembly = Assembly.LoadFrom(assemblyPath);



The interesting bit of code I needed to write was the GetFiles() method.  It needs to recursively iterate through files in a directory structure. Here is what I came up with.



      public static IEnumerable<string>  GetFiles(string path)


            foreach (string s in Directory.GetFiles(path, “*.dll”))


                  yield return s;



            foreach (string s in Directory.GetDirectories(path))


                  foreach (string s1 in GetFiles(s))


                        yield return s1;





First we enumerate through all the files in the directory and yield each one out (that turns flow of control back over the foreach statement that called it).  Once all the files are done, we start iterating through the directories.  For each directory we get all of its files and yield them out in the same way.  Notice the recursive call in that 2nd foreach statement.


I do recommend you take a look at the IL for this, it is very interesting.  With yield there is a clear separation between what you feed the compiler and the IL that comes out on the back end.  I believe this is a good, and necessary language advancement, but it is worth being aware of.


update: fixed yield link

Comments (7)

  1. Tarun Jain says:

    Your link to the yield keyword does not work !!

  2. Brad Abrams says:

    Some internal folks sent some more data about the performance implications of the recursive iterators I note above. Yes, the code is very clean and simple, but it hides a potentially disastrous performance problem depending on how you use it. Lots of instances get created and stay live without you ever seeing it. It is certainly possible to use yield here without the recursion; but I will leave that as an exercise for the reader.

    I understand MSDN will do an article soon that covers this in more depth… I link to it when it comes out.

  3. Recursively iterate through files in a directory structure

  4. Ring says:

    Oh! ! I like studying the key words, but have no achievements all the time! ! It seems that you are good!