添加目录对象

若要添加新的目录对象,请使用 DirectoryEntry 类的 Children 属性。此属性返回一个公开 Add 方法的 DirectoryEntries 对象。

若要添加对象,请绑定到要向其添加该对象的容器,在添加对象之后,调用 CommitChanges 以将对象从缓存保存到目录。下面的代码示例说明如何使用 Add 方法添加新对象。

Try
    ' Bind to the Users container, then add a new group and a new contact.
    Dim de As New DirectoryEntry("LDAP://CN=Users,DC=fabrikam,DC=com")

    ' Create a new group object in the local cache.
    Dim newGroup As DirectoryEntry = de.Children.Add("CN=Sales", "group")

    ' Active Directory requires the sAMAccountName property for all
    ' Security Principal objects beginning with Windows Server 2003
    ' servers, the sAMAccountName will automatically be generated when
    ' Commit() is called if no sAMAccountName is provided.
    newGroup.Properties("sAMAccountName").Value = "Sales"

    ' Save the new object to the server.
    newGroup.CommitChanges()

    ' Create and commit a new contact object.
    Dim newContact As DirectoryEntry = de.Children.Add("CN=New Contact", "contact")
    newContact.CommitChanges()

    ' Bind to the Computers container and add a new computer.
    Dim de01 As New DirectoryEntry("LDAP://CN=Computers,DC=fabrikam,DC=com")
    Dim newComputer As DirectoryEntry = de01.Children.Add("CN=New Computer", "computer")
    newComputer.CommitChanges()
Catch COMEx As COMException
        ' If a COMException is thrown, then the following code can catch the text of the error.
        ' For more information about handling COM exceptions, see Handling Errors.
        Console.WriteLine(COMEx.ErrorCode)
End Try
try
{
    // Bind to the Users container, add a new group and a new contact.
    DirectoryEntry de = new DirectoryEntry("LDAP://CN=Users,DC=fabrikam,DC=com");

    // Create a new group object in the local cache.
    DirectoryEntry newGroup = de.Children.Add("CN=Sales", "group");

    // Active Directory requires the sAMAccountName property for all
    // Security Principal objects beginning with Windows Server 2003
    // servers, the sAMAccountName will automatically be generated when
    // Commit() is called if no sAMAccountName is provided.
    newGroup.Properties["sAMAccountName"].Value = "Sales";

    // Save the new object to the server.
    newGroup.CommitChanges();

    // Create and add a new contact object.
    DirectoryEntry newContact = de.Children.Add("CN=New Contact", "contact");
    newContact.CommitChanges();

    // Bind to the Computers container and add a new computer.
    DirectoryEntry de01 = new DirectoryEntry("LDAP://CN=Computers,DC=fabrikam,DC=com");
    DirectoryEntry newComputer = de01.Children.Add("CN=New Computer", "computer");
    newGroup.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);
}

添加新对象后,使用 Exists 方法验证目录中的项。此方法在 DirectoryEntry 类中提供。下面的代码示例说明如何使用 Exists

If ds.Exists("LDAP://CN=Sales,CN=Users,DC=fabrikam,DC=com") = True Then
   Console.WriteLine("object exists")
Else
   Console.WriteLine("object does not exist")
End If
if (DirectoryEntry.Exists("LDAP://CN=Sales,CN=Users,DC=fabrikam,DC=com"))
    Console.WriteLine("object exists");
else
    Console.WriteLine("object does not exist");

另请参见

参考

System.DirectoryServices
DirectoryEntry
DirectoryEntries

概念

创建、删除、重命名和移动对象

Send comments about this topic to Microsoft.

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。