You can use the DirectoryEntry class to retrieve a node from an Active Directory hierarchy and modify its property content. An example would be changing an employee's personal information after the employee moves to a different area of the organization. After you modify a node, you must commit your changes in order for them to be saved to the tree. For more information, see Introduction to Active Directory Objects.
Note You must have administrator rights to a directory in order to modify any of its information.
To modify an object in the Active Directory hierarchy
- Create an instance of the DirectoryEntry component, binding it to the known object in the active directory hierarchy with the object's path. For more information, see Creating DirectoryEntry Component Instances.
' Visual Basic
Dim User As New System.DirectoryServices.DirectoryEntry( _
"LDAP://CN=nate sun, CN= users, DC=Sales, DC=CompanyA, DC=COM")
// C#
System.DirectoryServices.DirectoryEntry user =
new System.DirectoryServices.DirectoryEntry(
"LDAP://CN=nate sun/CN= users, DC=Sales, DC=CompanyA, DC=COM");
- Make the necessary changes to the object's properties by referring to it by name.
The following example shows how to change the office telephone number for an employee:
' Visual Basic
User.Properties("HomeNumber")(0) = "(425) 555-1212"
// C#
user.Properties["HomeNumber"][0]= "(425) 555-1212";
- Call the CommitChanges method save your modifications to the item in the directory.
' Visual Basic
User.CommitChanges()
// C#
user.CommitChanges();
See Also
Introduction to Active Directory Objects | Searching Active Directory Hierarchies | Walkthrough: Adding Active Directory Objects | Removing Active Directory Nodes