Поделиться через


Enumerating Properties Using System.DirectoryServices

You can use System.DirectoryServices to enumerate IIS metabase properties when you want to display configuration data that is stored at a specific metabase node.

Example Code

The following example shows you how to use the C# programming language to enumerate properties at a node in the IIS metabase.

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 enumerate metabase properties 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)
    {


...


EnumerateProperties("IIS://Localhost/W3SVC/1/Root");


...


}


...


static void EnumerateProperties(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("\nEnumerating properties for {0}:", metabasePath);

  try
  {
    DirectoryEntry entry = new DirectoryEntry(metabasePath);
    PropertyCollection props = entry.Properties;

    Console.WriteLine(" Total properties = {0}", props.Count);

    foreach (string propName in props.PropertyNames)
    {
      Console.Write(" {0} =", propName);
      foreach (object value in entry.Properties[propName])
      {
        Console.WriteLine("\t{0} \t({1})", value.ToString(), value.GetType());
      }
    }
    Console.WriteLine(" Done.");

  }
  catch (Exception ex)
  {
    Console.WriteLine("Failed in EnumeratePath with the following exception: \n{0}", ex.Message);
  }
}


...


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