Share via


DirectoryEntries.Remove (DirectoryEntry)

The Remove method deletes a child DirectoryEntry from this collection.

public void Remove(
  DirectoryEntry entry
);
Public Sub Remove( _
  ByVal entry As DirectoryEntry _
)

Parameters

  • entry
    Contains the name of the DirectoryEntry to delete.

Return Values

This method has no return values.

Exceptions

Exception type Condition
COMException An error occurred during the call to the underlying interface.

Remarks

The entry must be an empty container or a noncontainer. To delete an entry and all its children, use the DeleteTree method from DirectoryEntry.

Example Code [Visual Basic .NET]

The following example creates a new DirectoryEntry with the specified path, then creates a new entry in the container and saves it. Finally, it retrieves the newly created entry and deletes it.

Imports System
Imports System.DirectoryServices

Class MyClass1
   Shared Sub Main()
      Try
         Dim strPath As String = "IIS://localhost/W3SVC/1/Root"
         Dim strName As String = ""

         ' Create a new 'DirectoryEntry' with the given path.
         Dim myDE As New DirectoryEntry(strPath)
         Dim myEntries As DirectoryEntries = myDE.Children

         ' Create a new entry 'Sample' in the container.
         Dim myDirectoryEntry As DirectoryEntry = myEntries.Add("Sample", myDE.SchemaClassName)
         ' Save changes of entry in the 'Active Directory'.
         myDirectoryEntry.CommitChanges()
         Console.WriteLine(myDirectoryEntry.Name + " entry is created in container.")

         ' Find 'Sample' entry in container.
         myDirectoryEntry = myEntries.Find("Sample", myDE.SchemaClassName)
         Console.WriteLine(myDirectoryEntry.Name + " found in container.")
         ' Remove 'Sample' entry from container.
         strName = myDirectoryEntry.Name
         myEntries.Remove(myDirectoryEntry)
         Console.WriteLine(strName + " entry is removed from container.")

      Catch e As Exception
         Console.WriteLine("The following exception was raised : {0}", e.Message.ToString())
      End Try
   End Sub 'Main
End Class 'MyClass1

Example Code [C#]

The following example creates a new DirectoryEntry with the specified path, then creates a new entry in the container and saves it. Finally, it retrieves the newly created entry and deletes it.

using System;
using System.DirectoryServices; 

class MyClass1
{
   static void Main()
   {
      try
      {
         String strPath = "IIS://localhost/W3SVC/1/Root";
         String strName = "";

         // Create a new 'DirectoryEntry' with the given path.
         DirectoryEntry myDE = new DirectoryEntry(strPath);
         DirectoryEntries myEntries = myDE.Children;

         // Create a new entry 'Sample' in the container.
         DirectoryEntry myDirectoryEntry = 
            myEntries.Add("Sample", myDE.SchemaClassName);
         // Save changes of entry in the 'Active Directory'.
         myDirectoryEntry.CommitChanges();
         Console.WriteLine (myDirectoryEntry.Name + 
            " entry is created in container.");

         // Find 'Sample' entry in container.
         myDirectoryEntry = myEntries.Find("Sample", myDE.SchemaClassName);
         Console.WriteLine(myDirectoryEntry.Name + " found in container.");
         // Remove 'Sample' entry from container.
         strName = myDirectoryEntry.Name;
         myEntries.Remove(myDirectoryEntry);
         Console.WriteLine(strName+ " entry is removed from container.");

      }
      catch(Exception e)
      {
         Console.WriteLine("The following exception was raised : {0}",
            e.Message);
      }
   }
}

Example Code [Managed C++]

The following example creates a new DirectoryEntry with the specified path, then creates a new entry in the container and saves it. Finally, it retrieves the newly created entry and deletes it.

#using <mscorlib.dll>
#using <System.dll>
#using <System.Directoryservices.dll>

using namespace System;
using namespace System::DirectoryServices; 

int main() {
    try {
        String* strPath = S"IIS://localhost/W3SVC/1/Root";
        String* strName = S"";

        // Create a new 'DirectoryEntry' with the given path.
        DirectoryEntry* myDE = new DirectoryEntry(strPath);
        DirectoryEntries* myEntries = myDE->Children;

        // Create a new entry 'Sample' in the container.
        DirectoryEntry* myDirectoryEntry =  myEntries->Add(S"Sample", myDE->SchemaClassName);
        // Save changes of entry in the 'Active Directory'.
        myDirectoryEntry->CommitChanges();
        Console::WriteLine(S"{0} entry is created in container.", myDirectoryEntry->Name);

        // Find 'Sample' entry in container.
        myDirectoryEntry = myEntries->Find(S"Sample", myDE->SchemaClassName);
        Console::WriteLine(S"{0} found in container.", myDirectoryEntry->Name);
        // Remove 'Sample' entry from container.
        strName = myDirectoryEntry->Name;
        myEntries->Remove(myDirectoryEntry);
        Console::WriteLine(S"{0} entry is removed from container.", strName);
    } catch(Exception* e) {
        Console::WriteLine(S"The following exception was raised : {0}", e->Message);
    }
}

Requirements

Client Requires Windows XP Home Edition, Windows XP Professional, Windows Me, or Windows 98.
Server Requires Windows Server 2003, Windows 2000, or Windows NT 4.0.
Namespace

Defined in System.DirectoryServices.

Assembly

Requires System.DirectoryServices (in System.DirectoryServices.dll).

.NET Framework

Requires .NET Framework 1.0.

See Also

DirectoryEntries, DirectoryEntries Members (Visual J# Syntax), DirectoryEntries Methods, Managed Extensions for C++ Programming, System.DirectoryServices Namespace