简体   繁体   English

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

[英]how to get the application pool name for a specific website IIS6 programmatically? C#

how to get the application pool name for a specific website IIS 6 programmatic using C# 如何使用C#获取特定网站IIS 6程序的应用程序池名称

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

I don't agree with you. 我不同意你的看法。 I coded up a test app and I get the correct AppPool name from it, even if I set the AppPool manually using IIS Manager. 我编写了一个测试应用程序,我从中获得了正确的AppPool名称,即使我使用IIS管理器手动设置AppPool。

To make sure, I have tested once, name name was ok; 为了确保,我已经测试了一次,名字名称还可以; then, I popep up the IIS Manager, changed the AppPool, executed iisreset , and ran the test app again - the AppPool name I got was correct again. 然后,我找到了IIS管理器,更改了AppPool,执行了iisreset ,再次运行了测试应用程序 - 我得到的AppPool名称再次正确。 I don't how your code looked like, but mine is like this: 我不知道你的代码是怎样的,但我的代码是这样的:

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

Sorry for my lousy english. 抱歉我糟糕的英语。
Hope I've helped. 希望我帮了。

The classes in the System.DirectoryServices namespace will help you get that information. System.DirectoryServices命名空间中的类将帮助您获取该信息。

Check this article by Rick Strahl for an example: 请查看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;
}

In brief, there's 2 ways of doing this that spring to mind. 简而言之,有两种方法可以实现这一点。

The less sophisticated way is knowing that, IIS6's settings are stored in the MetaBase which is just an Xml file: 不太复杂的方法是知道,IIS6的设置存储在MetaBase中,它只是一个Xml文件:

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

You can just use Linq2Xml and parse the Xml looking for the sites name or Id, The AppPoolId attribute contains the name of the AppPool 您可以使用Linq2Xml并解析Xml以查找站点名称或Id,AppPoolId属性包含AppPool的名称

The proper way is to use System.DirectoryServices 正确的方法是使用System.DirectoryServices

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM