How to get the application pool name for a specific IIS6 website programmatically? FROM#

how to get application pool name for specific IIS 6 website using c #

EDIT: I have already used DirectoryServices namespace methods, but the application pool name is not retrieved correctly if it was not explicitly set using the same code. This means that if you add the site manually using the iis manager and install the application pool, these codes will not work (it will always return DefaultAppPool) anymore, when I create the application using sharepoint and install another appPool, these methods do not work.

+3
source share
3 answers

System.DirectoryServices .

:

/// <summary>
/// Returns a list of all the Application Pools configured
/// </summary>
/// <returns></returns>
public ApplicationPool[] GetApplicationPools()
{           
    if (ServerType != WebServerTypes.IIS6 && ServerType != WebServerTypes.IIS7)
        return null;

    DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools");
      if (root == null)
            return null;

    List<ApplicationPool> Pools = new List<ApplicationPool>();

    foreach (DirectoryEntry Entry in root.Children)
    {
        PropertyCollection Properties = Entry.Properties;

        ApplicationPool Pool = new ApplicationPool();
        Pool.Name = Entry.Name;

        Pools.Add(Pool);
    }

    return Pools.ToArray();
}

/// <summary>
/// Create a new Application Pool and return an instance of the entry
/// </summary>
/// <param name="AppPoolName"></param>
/// <returns></returns>
public DirectoryEntry CreateApplicationPool(string AppPoolName)
{
    if (this.ServerType != WebServerTypes.IIS6 && this.ServerType != WebServerTypes.IIS7)
        return null;

    DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools");
    if (root == null)
        return null;

    DirectoryEntry AppPool = root.Invoke("Create", "IIsApplicationPool", AppPoolName) as DirectoryEntry;           
    AppPool.CommitChanges();

    return AppPool;
}

/// <summary>
/// Returns an instance of an Application Pool
/// </summary>
/// <param name="AppPoolName"></param>
/// <returns></returns>
public DirectoryEntry GetApplicationPool(string AppPoolName)
{
    DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools/" + AppPoolName);
    return root;
}

/// <summary>
/// Retrieves an Adsi Node by its path. Abstracted for error handling
/// </summary>
/// <param name="Path">the ADSI path to retrieve: IIS://localhost/w3svc/root</param>
/// <returns>node or null</returns>
private DirectoryEntry GetDirectoryEntry(string Path)
{

    DirectoryEntry root = null;
    try
    {
        root = new DirectoryEntry(Path);
    }
    catch
    {
        this.SetError("Couldn't access node");
        return null;
    }
    if (root == null)
    {
        this.SetError("Couldn't access node");
        return null;
    }
    return root;
}
+1

. , AppPool, AppPool IIS.

, , ; IIS, AppPool, iisreset - AppPool, . , , :

using System;
using System.IO;
using System.DirectoryServices;

class Class
{
    static void Main(string[] args)
    {
        DirectoryEntry entry = FindVirtualDirectory("<Server>", "Default Web Site", "<WantedVirtualDir>");
        if (entry != null)
        {
            Console.WriteLine(entry.Properties["AppPoolId"].Value);
        }
    }

    static DirectoryEntry FindVirtualDirectory(string server, string website, string virtualdir)
    {
        DirectoryEntry siteEntry = null;
        DirectoryEntry rootEntry = null;
        try
        {
            siteEntry = FindWebSite(server, website);
            if (siteEntry == null)
            {
                return null;
            }

            rootEntry = siteEntry.Children.Find("ROOT", "IIsWebVirtualDir");
            if (rootEntry == null)
            {
                return null;

            }

            return rootEntry.Children.Find(virtualdir, "IIsWebVirtualDir");
        }
        catch (DirectoryNotFoundException ex)
        {
            return null;
        }
        finally
        {
            if (siteEntry != null) siteEntry.Dispose();
            if (rootEntry != null) rootEntry.Dispose();
        }
    }

    static DirectoryEntry FindWebSite(string server, string friendlyName)
    {
        string path = String.Format("IIS://{0}/W3SVC", server);

        using (DirectoryEntry w3svc = new DirectoryEntry(path))
        {
            foreach (DirectoryEntry entry in w3svc.Children)
            {
                if (entry.SchemaClassName == "IIsWebServer" &&
                    entry.Properties["ServerComment"].Value.Equals(friendlyName))
                {
                    return entry;
                }
            }
        }
        return null;
    }
}

.
, .

+7

, , spring .

A less complicated way is to know that IIS6 parameters are stored in the metabase, which is just an Xml file:

C:\WINDOWS\system32\inetsrv\MetaBase.xml

You can simply use Linq2Xml and parse the Xml looking for the site name or Id, the AppPoolId attribute contains the name AppPool

The correct way is to use System.DirectoryServices

+1
source

Source: https://habr.com/ru/post/1702690/


All Articles