Share via


Reading Properties on Directory Objects

When you retrieve property values for an object, the data is returned as an enumerable collection, even if only a single value is returned. This operation is performed with Properties, which is a property on the DirectoryEntry class. Properties returns a PropertyCollection object. The values for the properties referenced in the PropertyCollection are stored in the PropertyValueCollection object.

Note

The Properties property is not supported for use with the Active Directory Client Extension (DSClient) for Windows NT 4.0.

The property values in a collection are read using the Value property from the PropertyValueCollection object. If there is only one value in the collection, it is returned as an object representation of the value.

To access an object property value, provide the name of the property using the syntax shown in the following code example.

DirectoryEntry.Properties("givenName").Value
DirectoryEntry.Properties["givenName"].Value;

In this example, the code will access the givenName property, which is the LDAP display name for a property on the user object in Active Directory Domain Services and other LDAP directories. To access a specific property in the directory, provide the LDAP display name for that property in your application. For more information about the givenName property and the Active Directory user object, see the givenName topic and the User topic in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

The following code example shows how you could use the Properties collection to read a single value.

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);

See Also

Reference

System.DirectoryServices
DirectoryEntry
PropertyCollection
PropertyValueCollection

Concepts

Directory Object Properties

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.