Ben Konuyu biraz eski olduğunu biliyorum, ama son zamanlarda DirectorySearcher sağlar daha DirectoryEntries yoluyla manevra daha etkili bir yolunu oluşturulan ve bu Google'da ilk sonuç beri paylaşmak istedik. Bu örnek, OU yapısını başlangıçta belirtilen başlangıç noktasına göre çoğaltır.
ilk kurucu geçirilen DN yolu biçimi "LDAP: // OU = StartingOU, DC = test, DC = com" olmalıdır
using System.DirectoryServices;
using System.Threading.Tasks;
public class ADTree
{
DirectoryEntry rootOU = null;
string rootDN = string.Empty;
List<ADTree> childOUs = new List<ADTree>();
public DirectoryEntry RootOU
{
get { return rootOU; }
set { rootOU = value; }
}
public string RootDN
{
get { return rootDN; }
set { rootDN = value; }
}
public List<ADTree> ChildOUs
{
get { return childOUs; }
set { childOUs = value; }
}
public ADTree(string dn)
{
RootOU = new DirectoryEntry(dn);
RootDN = dn;
BuildADTree().Wait();
}
public ADTree(DirectoryEntry root)
{
RootOU = root;
RootDN = root.Path;
BuildADTree().Wait();
}
private Task BuildADTree()
{
return Task.Factory.StartNew(() =>
{
object locker = new object();
Parallel.ForEach(RootOU.Children.Cast<DirectoryEntry>().AsEnumerable(), child =>
{
if (child.SchemaClassname.Equals("organizationalUnit"))
{
ADTree ChildTree = new ADTree(child);
lock (locker)
{
ChildOUs.Add(ChildTree);
}
}
});
});
}
}
oluşturmak için, yapmanız gereken tek şey aşağıdaki:
ADTree Root = null;
Task BuildOUStructure = Task.Factory.StartNew(() =>
{
ADTree = new ADTree("LDAP://ou=test,dc=lab,dc=net");
});
BuildOUStructure.Wait();
, ** ** bu satırları vurgulamak lütfen i n metin editörü ve güzel bir format ve sözdizimi vurgulamak için editör araç çubuğundaki "kod örnekleri" düğmesine ('{}') tıklayın! –