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

static  Dictionary&lt;string,string&gt; pList=new Dictionary&lt;string, string&gt;(); 
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 &lt; 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:

folder-A
    folder-A-1
    folder-A-2
    folder-A-3
folder-B
    folder-B-1
    folder-B-2
    folder-B-3
folder-C
    folder-C-1
    folder-C-2
    folder-C-3


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?
To relate your parent directories to their children, you could have plist declared like this:

static Dictionary&lt;String, List&lt;String&gt;&gt; plist = new Dictionary&lt;string, List&lt;string&gt;&gt;();


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&lt;string&gt;());
pList[parent].Add(item.Name);


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.