Freigeben über


Hinzufügen von Verzeichnisobjekten

Verwenden Sie zum Hinzufügen von neuen Verzeichnisobjekten die Children-Eigenschaft der DirectoryEntry-Klasse. Diese Eigenschaft gibt ein DirectoryEntries-Objekt zurück, das die Add-Methode verfügbar macht.

Um ein Objekt hinzuzufügen, binden Sie das Objekt an den Container, zu dem es hinzugefügt werden soll. Rufen Sie nach dem Hinzufügen des Objekts CommitChanges auf, um das Objekt aus dem Cache im Verzeichnis zu speichern. Im folgenden Codebeispiel wird die Verwendung der Add-Methode zum Hinzufügen von neuen Objekten veranschaulicht.

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);
}

Verwenden Sie nach dem Hinzufügen eines neuen Objekts die Exists-Methode, um einen Eintrag im Verzeichnis zu überprüfen. Diese Methode wird in der DirectoryEntry-Klasse bereitgestellt. Im folgenden Codebeispiel wird die Verwendung von Exists veranschaulicht.

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");

Siehe auch

Referenz

System.DirectoryServices
DirectoryEntry
DirectoryEntries

Konzepte

Erstellen, Löschen, Umbenennen und Verschieben von Objekten

Send comments about this topic to Microsoft.

Copyright © 2007 Microsoft Corporation. Alle Rechte vorbehalten.