如何以编程方式获取特定网站IIS6的应用程序池名称? C#

时间:2009-02-04 13:08:15

标签: c# iis application-pool

如何使用C#

获取特定网站IIS 6编程的应用程序池名称

编辑: 我已经使用了DirectoryServices命名空间的方法,但是除非使用相同的代码显式设置,否则无法正确检索应用程序池名称。这意味着如果您使用iis管理器手动添加网站并设置应用程序池,那么当我使用sharepoint创建应用程序并设置不同的appPool这些方法不起作用时,这些代码将无法工作(它将始终返回DefaultAppPool)。

3 个答案:

答案 0 :(得分:7)

我不同意你的看法。我编写了一个测试应用程序,我从中获得了正确的AppPool名称,即使我使用IIS管理器手动设置AppPool。

为了确保,我已经测试了一次,名字名称还可以;然后,我找到了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;
    }
}

抱歉我糟糕的英语 希望我帮助过。

答案 1 :(得分:1)

System.DirectoryServices namespace中的课程将帮助您获取该信息。

检查this article by Rick Strahl以获取示例:

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

答案 2 :(得分:1)

简而言之,有两种方法可以解决这个问题。

不太复杂的方法是知道,IIS6的设置存储在MetaBase中,而MetaBase只是一个Xml文件:

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

你可以使用Linq2Xml并解析Xml寻找网站名称或Id,AppPoolId属性包含AppPool的名称

正确的方法是使用System.DirectoryServices