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:
Great, that is the easy part.
Now put this list of Directories into a fully qualified Treeview. :)
Exactly what I needed. Thanks for the tip.
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.
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?
great! thanxxx...
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/
Great!!!Thanks!!!
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
Thanks. Well it seems that recursion exist but they wrapped it for us :)
thanks buddy that little tip was better than cheese on toast ;-)))
Good code snippet.
Cheers!!!
This code saved my life :)
thank you very much...
There are some access denied directories and trying to get the directory gives an exception what should i do ?
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?
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
Thanks it's working.
Post a Comment