特定のフォルダーのツリー構造を作成しています(フォルダーのみファイルなし)また、各フォルダーのセキュリティ権限を返し、リストに追加します
では、このコンポジットをループして、すべてのアイテムとサブアイテムを取得するにはどうすればよいですか。
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?
}
}