读取目录对象的属性
检索对象的属性值时,数据作为可枚举的集合返回,即使仅返回单个值也是如此。此操作是使用 Properties(DirectoryEntry 类的一个属性)执行的。Properties 返回一个 PropertyCollection 对象。PropertyCollection 中引用属性的值存储在 PropertyValueCollection 对象中。
注意: |
---|
Properties 属性不能用于 Windows NT 4.0 的 Active Directory 客户端扩展 (DSClient)。 |
集合中的属性值是使用 PropertyValueCollection 对象中的 Value 属性读取的。如果集合中只有一个值,则它作为值的对象表示返回。
若要访问对象属性值,请使用下面的代码示例中所示的语法提供属性的名称。
DirectoryEntry.Properties("givenName").Value
DirectoryEntry.Properties["givenName"].Value;
在此示例中,代码将访问 givenName 属性,该属性是 Active Directory 域服务和其他 LDAP 目录中用户对象属性的 LDAP 显示名。若要访问目录中的特定属性,请在应用程序中提供该属性的 LDAP 显示名。有关 givenName 属性和 Active Directory 用户对象的更多信息,请参见位于 https://go.microsoft.com/fwlink/?LinkID=27252 上的 MSDN Library 中的 givenName(可能为英文网页)主题和用户(可能为英文网页)主题。
下面的代码示例说明如何使用 Properties 集合读取单个值。
Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com")
Dim name As [string] = ent.Properties("sn").Value.ToString()
Console.WriteLine(name)
DirectoryEntry ent = new DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com");
string name = ent.Properties["sn"].Value.ToString();
Console.WriteLine(name);
另请参见
参考
System.DirectoryServices
DirectoryEntry
PropertyCollection
PropertyValueCollection
概念
Send comments about this topic to Microsoft.
版权所有 (C) 2007 Microsoft Corporation。保留所有权利。