Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose I have a parent folder test in my c:\
I need to check how many sub folders are present in test folder and put them into dictionary of different levels

Folder Structure Like

test
test_sub1
test_sub1_sub11
test_sub2
test_sub2_sub11

C#
Dictionary<Key,Dictionary> dict=Dictionary<Key,Dictionary>();

for first sub folder it will be
C#
Dictionary<test,Dictionary<test_sub1,Dictionary<test_sub1_sub11,Value>>>

And for second as well
Posted
Updated 17-Jan-13 2:04am
v2
Comments
Suvabrata Roy 17-Jan-13 8:05am    
Self reference will solve your problem...

1 solution

Formally, what you need is a n-ary tree. Nesting dictionaries would be messy and unnecessary, in c# you can do something like the following

C#
public class DirectoryThing
{
    public Whatever PropertyOfDirectoryIWantToKeep {get; set;}
    public List<subdirectorythings> {get; private set}
}</subdirectorythings>

With proper naming :) etc and the addition of any methods/indexers you want to get infotmation in/out. It should be pretty easy to write a converter to work with the .net framework's DirectoryInfo class. In some circumstances you might even just want to use the DirectoryInfo type. For example if you are just counting subdirectories you can instantiate the root directory's DirectoryInfo class, use its GetDirectories[^] method to get the children and recursively traverse the whole tree, keeping a running total of the number of subdirectories until the tree is traversed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900