Directory Services |
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.
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.
[C#]
DirectoryEntry.Properties["givenName"].Value;
[Visual Basic .NET]
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 and other LDAP directories. To access a specific property in the directory, provide the LDAP display name for that property in your application.
The following code example shows how you could use the Properties collection to read a single value.
[C#]
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);
}
[Visual Basic .NET]
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()
If (True) Then
Console.WriteLine(name)
End If