sé que este hilo es un poco viejo, pero recientemente he creado una manera más eficiente de maniobrar entre DirectoryEntries que el DirectorySearcher ofrece y quería compartir ya que este fue el primer resultado en Google. Este ejemplo replica la estructura de OU en función de un punto de inicio inicialmente especificado.
La ruta DN pasado al primer constructor debe estar en el formato de "LDAP: // OU = StartingOU, DC = prueba, DC = com"
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);
}
}
});
});
}
}
para construir, todo lo que tiene que hacer es los siguientes:
ADTree Root = null;
Task BuildOUStructure = Task.Factory.StartNew(() =>
{
ADTree = new ADTree("LDAP://ou=test,dc=lab,dc=net");
});
BuildOUStructure.Wait();
Si publica ejemplos de código, XML o de datos, por favor ** ** destacar aquellas líneas i n el editor de texto y haga clic en el botón "muestras de código" ('{}') en la barra de herramientas del editor para formatear y sintaxis y ¡resaltarlo! –