Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

DirectoryEntries.Remove Method

Deletes a member of this collection.

Namespace:  System.DirectoryServices
Assembly:  System.DirectoryServices (in System.DirectoryServices.dll)
public void Remove(
	DirectoryEntry entry
)

Parameters

entry
Type: System.DirectoryServices.DirectoryEntry

The name of the DirectoryEntry object to delete.

ExceptionCondition
COMException

An error occurred during the call to the underlying interface.

If the entry to be removed is a container, the container must be empty. To delete a container and all its children, use the DeleteTree method.

The following Visual Basic .NET example creates a new DirectoryEntry object with the specified path, then creates a new entry in the container and saves it. Finally, it retrieves the new 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' object 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 Domain Services'.
         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

The following C# example creates a new DirectoryEntry object with the specified path, then creates a new entry in the container and saves it. Finally, it retrieves the new 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' object 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 Domain Services'.
         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);
      }
   }
}

The following C++ example creates a new DirectoryEntry object with the specified path, then creates a new entry in the container and saves it. Finally, it retrieves the new 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' object 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 Domain Services'.
        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);
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.