The below code is recursive function that list directories in 2 levels:

static  Dictionary<string,string> pList=new Dictionary<string, string>(); 
static void ListDirec(string path, int start, int end)
    var dirInfo = new DirectoryInfo(path);
    var folders = dirInfo.GetDirectories().ToList();

    foreach (var item in folders)
        Console.WriteLine("".PadLeft(start * 4, ' ') + item.Name);
        if (start < end) {
            ListDirec(item.FullName, start + 1, end);

Now how can optimize that in this case?

For example, I've these directories with their sub-directories:


Now I want save parents as key and children as value in dictionary, with their relation for example folder-A has these children: folder-A-1, folder-A-2, folder-A-3 and so on.

What's the best way to do that in this function?

  • \$\begingroup\$ You are asking for help in writing code to add the data to a dictionary. This code does not exist yet, so it is not ready for a Code Review. When your code works as designed, bring it back for review. \$\endgroup\$ – rolfl Apr 16 '14 at 21:27

To relate your parent directories to their children, you could have plist declared like this:

static Dictionary<String, List<String>> plist = new Dictionary<string, List<string>>();

Each time you hit a child folder, you add its name to

Console.WriteLine("".PadLeft(start * 4, ' ') + item.Name);
string parent = path.Split('\\').Last();
if (!pList.Keys.Contains(parent))
    pList.Add(parent, new List<string>());

Sorry, I don't have access to a C# compiler at the moment so I can't verify this 100%, but it's a starting point at least.

Edit: After thinking about it, I don't think C# likes it if you use [] before an element is in the Dictionary. I edited the above code to include how to get the parent and included the Console.WriteLine so you know where I would put it.


One option is to use a Dictionary<DirectoryInfo, List<DirectoryInfo>>. This way if you want to access the directories the methods you need are right there:

public static Dictionary<DirectoryInfo, List<DirectoryInfo>> BuildTree(string path)
    Dictionary<DirectoryInfo, List<DirectoryInfo>> temptree = new Dictionary<DirectoryInfo, List<DirectoryInfo>>();
    //If you need to filter the results edit the search string
    foreach(DirectoryInfo dir in new DirectoryInfo(path).GetDirectories("*", SearchOption.TopDirectoryOnly))
        temptree.Add(dir, dir.GetDirectories("*", SearchOption.TopDirectoryOnly).ToList());
    return temptree;

If you would like a LINQ option you could do it this way:

public Dictionary<DirectoryInfo, List<DirectoryInfo>> BuildTree(string path)
    Dictionary<DirectoryInfo, List<DirectoryInfo>> temptree = (from dir in new DirectoryInfo(path).EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
                                                                select dir).ToDictionary(x => x, x => x.GetDirectories("*", SearchOption.TopDirectoryOnly).ToList());
    return temptree;

Not the answer you're looking for? Browse other questions tagged or ask your own question.