Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Java
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
 
Dictionary<Key,Dictionary> dict=Dictionary<Key,Dictionary>();
for first sub folder it will be
Dictionary<test,Dictionary<test_sub1,Dictionary<test_sub1_sub11,Value>>>
And for second as well
Posted 17-Jan-13 2:43am
Edited 17-Jan-13 3:04am
v2
Comments
Suvabrata Roy at 17-Jan-13 8:05am
   
Self reference will solve your problem...

1 solution

Rate this: bad
good
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}
}</subdirectorythings>
With proper naming Smile | :) 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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 295
2 Sergey Alexandrovich Kryukov 197
3 Peter Leow 155
4 Afzaal Ahmad Zeeshan 154
0 OriginalGriff 7,510
1 DamithSL 5,494
2 Sergey Alexandrovich Kryukov 4,954
3 Maciej Los 4,936
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
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