Partager via


Setting Properties on Directory Objects

This topic explains and provides code examples for setting properties with single values for directory objects.

Use the following methods to modify property values:

  • Value is a property of the PropertyValueCollection object that sets a new value directly to the property for properties that contain a single value.
  • Add is a method of the PropertyValueCollection object that adds a value to a multi-valued property. For more information on multi-valued properties, see Properties with Multiple Values.

When you set a property value, the data is saved in the property cache. To write the new data to the directory, call the CommitChanges method. For more information, see The Property Cache.

The following code example shows how to use the Value property.

Try
    Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com")
    ent.Properties("sn").Value = "Barr"
    ent.CommitChanges()
Catch COMEx As COMException
    ' If a COMException is thrown, then the following code example can catch the text of the error.
    ' For more information about handling COM exceptions, see Handling Errors.
    Console.WriteLine(COMEx.ErrorCode)
End Try
try
{
    DirectoryEntry ent = new DirectoryEntry(
        "LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com");
    ent.Properties["sn"].Value = "Barr";
    ent.CommitChanges();
}
catch (COMException COMEx)
{
    // If a COMException is thrown, then the following code example can catch the text of the error.
    // For more information about handling COM exceptions, see Handling Errors.
    Console.WriteLine(COMEx.ErrorCode);
}

See Also

Reference

System.DirectoryServices
PropertyValueCollection
DirectoryEntry

Concepts

Directory Object Properties
The Property Cache
Properties with Multiple Values

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.