2011-04-04 102 views
1

我有一个代码来获取域中OU的列表。获取OU的完全限定名称

现在这只是列出所有的OU,并没有给出任何方式来区分一个OU和一个子OU。

DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain); 

DirectorySearcher mySearcher = new DirectorySearcher(entry); 
mySearcher.Filter = ("(objectClass=organizationalUnit)"); 

foreach (SearchResult temp in mySearcher.FindAll()) 
{ 
    OU_DownList.Items.Add(temp.Properties["name"][0].ToString()); 
} 

有没有一种方法可以得到OU的完全限定名?

事情是这样的一个子OU:

CN=Computer1,OU=Department 101,OU=Business Unit #1,DC=us,DC=xyz,DC=com 

任何帮助表示赞赏...谢谢

回答

5

temp.Path应该让你的每个OU的distinguishedName来。

+0

嘿感谢哥们但有显示以上完全合格的名称的方式“CN =计算机1,OU =部门101, OU = Business Unit#1,DC = us,DC = xyz,DC = com“as .. businessmenit/department 101 – user175084 2011-04-04 21:59:07

+0

'string.Join(”/“,temp.Path.Split(',')。Select(s => s.StartsWith(“OU”))。Reverse()。ToArray())' – 2011-04-05 16:21:16

1

使用属性Path from SearchResult,如temp.Path,请参阅link

Path属性唯一标识 Active Directory中的此条目 层次结构。该条目始终可以使用此路径检索到 。

您可以枚举所有与下面的源代码可用的属性:

foreach(string propKey in temp.Properties.PropertyNames) 
{ 
    // Display each of the values for the property identified by 
    // the property name. 
    foreach (object property in temp.Properties[propKey]) 
    { 
     Console.WriteLine("{0}:{1}", propKey, property.ToString()); 
    } 
} 
+0

嘿感谢这么多..有没有办法显示上述字符串作为businessunit /部门101 – user175084 2011-04-04 21:55:59

相关问题