Tuesday, 15 January 2013

c# - Get all folders from Composite -



c# - Get all folders from Composite -

i'm creating tree construction of given folder (only folders no files) homecoming security permission of each folder , add together list

now how can loop through composite , items , sub items

public abstract class foldercomponent:ienumerable { public string fullname { get; set; } public string name { get; set; } public list<string[]> rules { get; set; } public abstract void addfolder(foldercomponent foldercomponent); public abstract ienumerator getenumerator(); public abstract void assignrules(); } public class folder : foldercomponent { public ilist<foldercomponent> foldercomponents { get; set; } public folder(string path) { fullname = path; foldercomponents = new list<foldercomponent>(); rules = new list<string[]>(); } public override void addfolder(foldercomponent foldercomponent) { foldercomponents.add(foldercomponent); } public override ienumerator getenumerator() { homecoming foldercomponents.getenumerator(); } public override void assignrules() { // code string[] rules = new string[]{"read","write","execute"}; rules.add(rules); } } public class programme { private static foldercomponent getfolders(string path) { foldercomponent folder = new folder(path); folder.assignrules(); foreach (var directory in directory.getdirectories(path)) { folder.addfolder(getfolders(directory)); } homecoming folder; } public static void main() { foldercomponent folder = getfolders(@"c\:test"); // how can loop though folder construction within folder? } }

if want folder class:

foldercomponent folders = getfolders(@"c\:test"); foreach (var folder in folders) { console.writeline(folder.fullname); }

and total directory tree printed, getenumerator method needs have logic traverse tree. example

public override ienumerator<foldercomponent> getenumerator() { // homecoming current element yield homecoming this; // work through collection of components in folder ... foreach (var component in foldercomponents) { // ... , traverse subfolders in each component. // note recursively calls getenumerator // method on subfolders work downwards tree. foreach (var subfolder in component) { yield homecoming subfolder; } } }

c#

No comments:

Post a Comment