Using the DirectoryEntries class and collection, you can add objects to the Active Directory. This following example demonstrates creating a new User object in the Active Directory.
| Caution |
When writing new objects to Active Directory, it is important to remember that you are working in the client's memory and that the changes are not propagated to the server until you call CommitChanges. |
using System;
using System.DirectoryServices;
namespace Chapter12
{
public class AddingObjectsToTheDirectory
{
public AddingObjectsToTheDirectory()
{
DirectoryEntry MyObject = new DirectoryEntry();
MyObject.Path =
"LDAP://HMSRevenge/OU=Users,DC=Test,DC=com";
DirectoryEntries users = MyObject.Children;
//Creates a new user object
DirectoryEntry NewUser =
users.Add("Greg MacBeth", "user");
//Modifies the properties of an object
NewUser.Properties["company"].Add
("Microsoft Corporation");
NewUser.Properties["employeeID"].Add("1001");
NewUser.Properties["userPassword"].Add("Password");
NewUser.CommitChanges();
}
}
}