Tuesday, January 01, 2008

Get all subdirectories in a single line of C# code

One of the necessary evils of programming has been the need to write recursive routines in order to discover all the sub-folders and files on a file system based on a specified parent folder. Recursion is the practice of writing a routine that calls itself. In the case of "walking" a directory tree, a method can look at a folder, list all the files and folders in that folder, process each file, and then call itself for each of the folders. Recursion is not hard, it is just something that make you go "hmmmm" each time you do it.

My latest discovery is that I can now get all the subfolders of a parent folder with a single line of .Net code, returned as a string array. Then simply write a foreach loop that processes all files found in each of the fully-specified folders. No more recursion and it works like a charm. Can't stand the suspense? Here is the line of C# code that does it:

string[] folders = System.IO.Directory.GetDirectories(@"C:\My Sample Path\","*", System.IO.SearchOption.AllDirectories);

Woot!!

I would say that you can now forget recursion, but it is still a popular question in programming job interviews and certification tests, so you still need to get comfortable with it.

Hope that helps.

Joe Kunk
Okemos, MI USA

16 comments:

Anonymous said...

Great, that is the easy part.
Now put this list of Directories into a fully qualified Treeview. :)

Anonymous said...

Exactly what I needed. Thanks for the tip.

Joe Kunk said...

Thank you for your comments.

I understand that populating a Treeview with directories is non-trivial. Isn't that the way it always seems to go - as soon as one thing becomes easy, the next "hard" thing is right there waiting to challenge us?

I try to post about the things that I run into, in the hopes that others are likely to run into the same things and that my comments can be helpful in some way, even when they border on the obvious to some.

I think I have been successful so far in that effort and I plan to continue to offer helpful tips, large and small, whenever I can.

Anonymous said...

Forgive the dumb question - I'm just starting out - but is there some way to use the current directory without knowing what the path to the current directory is?

Anonymous said...

great! thanxxx...

Johan Idstam said...

Directory.GetFiles also has the search options parameter so you don't have to do any iteration at all to get all sub files.

/johan/

Anonymous said...

Great!!!Thanks!!!

Anonymous said...

ohhhh nice code...i still have one question, i would like to have them by hierarchy...like a treeview...how could i achieve that?...Thanks

Anonymous said...

Thanks. Well it seems that recursion exist but they wrapped it for us :)

BigGorilla said...

thanks buddy that little tip was better than cheese on toast ;-)))

Anonymous said...

Good code snippet.
Cheers!!!

Hasan said...

This code saved my life :)
thank you very much...

Anonymous said...

There are some access denied directories and trying to get the directory gives an exception what should i do ?

Anonymous said...

hello sir,

I'm new to .net, I want to know about Buffer like when to use it? why to use? how to write code?

Alex said...

This method isn't really fast.
If you're looking for Performance do the recursion by your own.

i used something like this in my program:

foreach (string d in Directory.GetDirectories(sdir))
{
foreach (string f in Directory.GetFiles(d))
{
//Code to do

Anonymous said...

Thanks it's working.