我正在创建给定文件夹的树结构(只有文件夹没有文件) 我也会返回每个文件夹的安全权限并将其添加到列表中
现在我怎样才能遍历此复合并获取所有项目和子项目
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?
}
}
答案 0 :(得分: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;
}
}
}
答案 1 :(得分: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();
}
答案 2 :(得分: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);
}