Amr Elsehemy Amr Elsehemy - 2 months ago 23
C# Question

how to get the application pool name for a specific website IIS6 programaticaly? C#

how to get the application pool name for a specific website IIS 6 programmatic using C#

EDIT :
I already used the methods of DirectoryServices namespace but the application pool name isn't retrieved correctly unless it was explicitly set by using the same code. Which means if u add a website manually using the iis manager and set an application pool, those codes won't work (it will always return DefaultAppPool) more over when I create an application using sharepoint and set a different appPool those methods dont work.

M4N M4N
Answer

The classes in the System.DirectoryServices namespace will help you get that information.

Check this article by Rick Strahl for an example:

/// <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;
}
Comments