subdirectories
-
hey everyone, I've been searching for a way to get an array that includes every directory within a selected directory. say the parent directory was called "hello", I want to get every directory, even the directories within the directories returned. (i.e. hello/hieveryone/helloagain/). There is a "Directory.GetDirectories" method, but this only returns the directories immediately below the selected one. How do I get all the ones below that one, and below that one and send them to an array? all the levels below would be nice, but atleast a minimum of 12 levels below the directory. sry for the wordiness, I didn't know how else to word it. thanks a lot for your help!
-
hey everyone, I've been searching for a way to get an array that includes every directory within a selected directory. say the parent directory was called "hello", I want to get every directory, even the directories within the directories returned. (i.e. hello/hieveryone/helloagain/). There is a "Directory.GetDirectories" method, but this only returns the directories immediately below the selected one. How do I get all the ones below that one, and below that one and send them to an array? all the levels below would be nice, but atleast a minimum of 12 levels below the directory. sry for the wordiness, I didn't know how else to word it. thanks a lot for your help!
You need to write a recursive function, that is, one that 'collects' all the directories within your main one, and calls itself for each, so it keeps drilling down. Christian Graus - Microsoft MVP - C++
-
hey everyone, I've been searching for a way to get an array that includes every directory within a selected directory. say the parent directory was called "hello", I want to get every directory, even the directories within the directories returned. (i.e. hello/hieveryone/helloagain/). There is a "Directory.GetDirectories" method, but this only returns the directories immediately below the selected one. How do I get all the ones below that one, and below that one and send them to an array? all the levels below would be nice, but atleast a minimum of 12 levels below the directory. sry for the wordiness, I didn't know how else to word it. thanks a lot for your help!
Pyro Joe wrote: How do I get all the ones below that one, and below that one and send them to an array? By doing a recursive call like this:
public ArrayList Directories(DirectoryInfo startDir)
{
ArrayList list = new ArrayList();
DirectoryInfo[] dirs = startDir.GetDirectories();
foreach(DirectoryInfo dir in dirs)
{
list.Add(dir);
list.AddRange(Directories(dir));
}
}See how the Directories() calls itself, but for the subdirectory. Does this help? DISCLAIMER: I typed the code directly in so there may be some syntax errors
My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
-
Pyro Joe wrote: How do I get all the ones below that one, and below that one and send them to an array? By doing a recursive call like this:
public ArrayList Directories(DirectoryInfo startDir)
{
ArrayList list = new ArrayList();
DirectoryInfo[] dirs = startDir.GetDirectories();
foreach(DirectoryInfo dir in dirs)
{
list.Add(dir);
list.AddRange(Directories(dir));
}
}See how the Directories() calls itself, but for the subdirectory. Does this help? DISCLAIMER: I typed the code directly in so there may be some syntax errors
My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
-
yeah, thanks! I was gonna try to use a while loop, but that could get really complex logic-wise. but the foreach thing is really handy. thanks!