Compartilhar via


Deleting Metabase Nodes Using System.DirectoryServices

You can delete a metabase node such as a virtual directory to remove it from an IIS server. Deleting a metabase node changes only the configuration of the server. It does not delete the content files and DLLs that are stored in the physical path that is mapped to the virtual directory.

Example Code

The following example shows you how to use the C# programming language to delete a node in the IIS metabase. In this case, the node is a Web site with the identifier of 555. To see an example of code that creates a Web site, see Creating Sites and Virtual Directories Using System.DirectoryServices.

This example requires Windows XP Professional Service Pack 2 or Windows Server 2003 Service Pack 1.

Note

System.DirectoryServices can be used to get and set String and DWORD properties in the IIS metabase, and invoke most methods. However, you cannot delete metabase nodes unless you are using Windows XP Professional with Service Pack 2 or Windows Server 2003 with Service Pack 1.

To keep this code example concise, it does not include code access security (CAS) parameters or parameter checking. For more information, see Code Access Security and Validating User Input to Avoid Attacks. Additionally, you can instantiate your System.DirectoryServices.DirectoryEntry object with an authentication parameter.

using System;
using System.IO;
using System.DirectoryServices;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;

namespace System_DirectoryServices_DirectoryEntry_ConfigIIS
{
  class Program
  {
    static void Main(string[] args)
    {


...


DeleteTree("IIS://Localhost/W3SVC/555");


...


}


...


static void DeleteTree(string metabasePath)
{
  //  metabasePath is of the form "IIS://<servername>/<path>"
  //    for example "IIS://localhost/W3SVC/1/Root/MyVDir" 
  //    or "IIS://localhost/W3SVC/AppPools/MyAppPool"
  Console.WriteLine("\nDeleting {0}:", metabasePath);

  try
  {
    DirectoryEntry tree = new DirectoryEntry(metabasePath);
    tree.DeleteTree();
    tree.CommitChanges();
    Console.WriteLine(" Done.");
  }
  catch (DirectoryNotFoundException ex)
  {
    Console.WriteLine(" Done.");
  }
  catch (Exception ex)
  {
    Console.WriteLine("Failed in DeleteTree with the following exception: \n{0}", ex);
    Console.WriteLine("Could not delete metabase path {0}", metabasePath);
  }
}


...


  }
}
Imports System
Imports System.IO
Imports System.DirectoryServices
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Collections

Module Program

    Sub Main(ByVal args() As String)


...


End Sub


...


End Module