Click here to Skip to main content
13,088,846 members (53,010 online)
Rate this:
Please Sign up or sign in to vote.
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


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

for first sub folder it will be

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

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

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

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100