DirectoryEntries.Remove(DirectoryEntry) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
刪除此收藏中的一位成員。
public:
void Remove(System::DirectoryServices::DirectoryEntry ^ entry);
public void Remove(System.DirectoryServices.DirectoryEntry entry);
member this.Remove : System.DirectoryServices.DirectoryEntry -> unit
Public Sub Remove (entry As DirectoryEntry)
參數
- entry
- DirectoryEntry
要刪除的 DirectoryEntry 物件名稱。
例外狀況
在呼叫底層介面時發生錯誤。
範例
以下範例會建立 DirectoryEntry 一個包含指定路徑的新物件,然後在容器中建立新項目並儲存。 最後,它會取得新條目並將其刪除。
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
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);
}
}
}
備註
若要移除的入口為容器,容器必須是空的。 要刪除容器及其所有子節點,請使用該 DeleteTree 方法。