Along with adding objects and setting their properties, you may also want to set the attributes for new Active Directory objects or modify the attributes of existing objects. The following examples demonstrate how to write new attributes to an object and modify an object's attributes.
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;
DirectoryEntry NewUser = users.Add("Greg MacBeth", "user");
NewUser.Properties["company"].Add("Microsoft Corporation");
NewUser.Properties["employeeID"].Add("1001");
NewUser.Properties["userPassword"].Add("Password");
//Writes values to an object
NewUser.CommitChanges();
}
}
}
using System; using System.DirectoryServices; namespace Chapter12 { public class ModifyingExistingObjectAttributes { public ModifyingExistingObjectAttributes() { DirectoryEntry MyDirectoryObject = new DirectoryEntry(); MyDirectoryObject.Path = "LDAP://HMSRevenge/CN=gregmcb,OU=users,DC=Test,DC=com"; MyDirectoryObject.Username = @"Test\gregmcb"; MyDirectoryObject.Password = @"MyPassword"; if(MyDirectoryObject.Properties[("company")].Value == "Old Company Name. inc.") { MyDirectoryObject.Properties[("company")][0] = "Microsoft Corporation"; } MyDirectoryObject.CommitChanges(); } } }