共用方式為


Enumerating Applications in a Pool Using System.DirectoryServices

Calling methods of the unmanaged IIS ADSI provider in managed code using System.DirectoryServices is often complicated. The following code example uses the Invoke method of the DirectoryEntry class to call the IIS ADSI IIsApplicationPool.EnumAppsInPool (ADSI) method. The IIsApplicationPool.EnumAppsInPool (ADSI) method returns the list of applications in a parameter of the method, but because of the way the call is marshaled to the Invoke method, the list of applications is returned in an object array - not in the parameter list.

Example Code

The following example shows you how to use the C# programming language to enumerate the friendly names of the applications that are contained in the specified application pool.

Note

In IIS 5.1 and earlier versions, application pools are not available.

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)
    {


...


EnumerateApplicationsInPool("IIS://localhost/W3SVC/AppPools/DefaultAppPool");


...


}


...


static void EnumerateApplicationsInPool(string metabasePath)
{
    //  metabasePath is of the form "IIS://<servername>/W3SVC/AppPools/<poolName>"
    //    for example "IIS://localhost/W3SVC/AppPools/DefaultAppPool" 
    Console.WriteLine("\nEnumerating applications for the {0} pool:", metabasePath);

    try
    {
        DirectoryEntry entry = new DirectoryEntry(metabasePath);

        if ("IIsApplicationPool" == entry.SchemaClassName)
        {
            object[] param;
            param = (object [])entry.Invoke("EnumAppsInPool", null);
            foreach (string s in param) Console.WriteLine("  {0}", s);
            Console.WriteLine(" Done.");
        }
        else
            Console.WriteLine(" Failed in EnumerateApplicationsInPool; {0} is not an app pool", metabasePath);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed in EnumerateApplicationsInPool with the following exception: \n{0}", ex);
    }
}


...


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