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 回答