1

特定のフォルダーのツリー構造を作成しています(フォルダーのみファイルなし)また、各フォルダーのセキュリティ権限を返し、リストに追加します

では、このコンポジットをループして、すべてのアイテムとサブアイテムを取得するにはどうすればよいですか。

 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()
    {
        return FolderComponents.GetEnumerator();
    }

    public override void AssignRules()
    {
        // some code
        string[] rules = new string[]{"Read","Write","Execute"};
        Rules.Add(rules);
    }

}

public class Program
{
    private static FolderComponent GetFolders(string path)
    {
        FolderComponent folder = new Folder(path);
        folder.AssignRules();
        foreach (var directory in Directory.GetDirectories(path))
        {
            folder.AddFolder(GetFolders(directory));
        }
        return folder;
    }

    public static void Main()
    {

        FolderComponent folder = GetFolders(@"C\:Test");
        // How can i loop though all folder structure inside folder?


    }
}
4

3 に答える 3

2

クラスでこのようなことをしたい場合Folder

FolderComponent folders = GetFolders(@"C\:Test");
foreach (var folder in folders) { Console.WriteLine(folder.FullName); }

ディレクトリツリー全体を印刷する場合、GetEnumeratorメソッドにはツリーをトラバースするロジックが必要です。例えば

    public override IEnumerator<FolderComponent> GetEnumerator()
    {
        // Return the current element
        yield return this;

        // Work through the collection of components in this folder ...
        foreach (var component in FolderComponents)
        {
            // ... and traverse all the subfolders in each component.  
            // Note that this recursively calls this GetEnumerator
            // method on the subfolders to work down the tree.
            foreach (var subfolder in component)
            {
                yield return subfolder;
            }
        }
    }
于 2013-02-06T11:10:29.843 に答える
1

私はあなたのFolderComponentクラスにメソッドを追加し、それがそれができるかどうかを確認するために実装しました:

 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 abstract List<FolderComponent> GetAllItems();
}

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()
    {
        return FolderComponents.GetEnumerator();
    }

    public override void AssignRules()
    {
        // some code
        string[] rules = new string[] { "Read", "Write", "Execute" };
        Rules.Add(rules);
    }

    public override List<FolderComponent> GetAllItems()
    {
        var resultItems = new List<FolderComponent> {this};
        foreach (var folderComponent in FolderComponents)
        {
            resultItems.AddRange(folderComponent.GetAllItems());
        }                        
        return resultItems;
    }
}

public class Program
{
    private static FolderComponent GetFolders(string path)
    {
        FolderComponent folder = new Folder(path);
        folder.AssignRules();
        foreach (var directory in Directory.GetDirectories(path))
        {
            folder.AddFolder(GetFolders(directory));
        }
        return folder;
    }

    public static void Main()
    {

        FolderComponent rootfolder = GetFolders(@"D:\4Share");
        var allItems = rootfolder.GetAllItems();
        foreach (var folderComponent in allItems)
        {
            Console.WriteLine(folderComponent.FullName);                
        }
        Console.ReadLine();

    }
于 2013-02-06T12:15:52.980 に答える
0

フォルダタイプ(子)のリストを追加する必要があります。

FolderComponent Parent;
List<FolderComponent> children;

そして、特定のフォルダ内にフォルダが見つかったら、子を追加します。逆方向処理のために親を追加することもできます。

var directoryInfo = new DirectoryInfo(folderPath);
foreach (var directory in directoryInfo)
{
    FolderComponent newSubFolder = new FolderComponent();
    newSubFolder.Parent = this;
    // get whatever you want like name, permissions etc.
    children.Add(newSubFolder);
}

コードにはこのようなものが必要です(実装によって異なります)

PrintFolders()
{
    foreach(var child in children)
         child.PrintFolders();

    Console.WriteLine(FolderName);
}
于 2013-02-06T10:36:20.240 に答える