I have a DataGridView which displays all group members of our ActiveDirectory. When double-clicking on a group I want to show all members and sub-members of this group (including users) in a treeView. I have made several attempts to do this but my problem is that the procedure needs to run until all members and sub-members have been added to the treeView, which I am unable to do. Is there some kind of pattern that I need to use for this? Starting from this, how should my code look like?
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
GroupPrincipal gp = GroupPrincipal.FindByIdentity(ctx, name);
var nodes = new List<TreeNode>();
foreach (Principal p in gp.GetMembers())
{
nodes.Add(new TreeNode(p.Name));
}
treeView.Nodes.AddRange(nodes.ToArray());