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

如何以编程方式获取特定网站IIS6的应用程序池名称?C#,c#,iis,application-pool,C#,Iis,Application Pool,如何使用C获取特定网站IIS 6的应用程序池名称# 编辑: 我已经使用了DirectoryServices命名空间的方法,但除非使用相同的代码显式设置应用程序池名称,否则无法正确检索该名称。这意味着,如果您使用iis管理器手动添加网站并设置应用程序池,那么当我使用sharepoint创建应用程序并设置其他应用程序池时,这些代码将不再有效(它将始终返回DefaultAppPool)。这些方法不起作用。中的类将帮助您获得这些信息 查看以下示例: // ///返回已配置的所有应用程序池的列表 ///

如何使用C获取特定网站IIS 6的应用程序池名称#

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

中的类将帮助您获得这些信息

查看以下示例:

//
///返回已配置的所有应用程序池的列表
/// 
/// 
公共应用程序池[]GetApplicationPools()
{           
if(ServerType!=WebServerTypes.IIS6&&ServerType!=WebServerTypes.IIS7)
返回null;
DirectoryEntry root=this.GetDirectoryEntry(“IIS://“+this.DomainName+”/W3SVC/AppPools”);
if(root==null)
返回null;
列表池=新列表();
foreach(root.Children中的DirectoryEntry)
{
PropertyCollection属性=Entry.Properties;
ApplicationPool池=新的ApplicationPool();
Pool.Name=Entry.Name;
池。添加(池);
}
返回池。ToArray();
}
/// 
///创建一个新的应用程序池并返回条目的实例
/// 
/// 
/// 
public DirectoryEntry CreateApplicationPool(字符串AppPoolName)
{
if(this.ServerType!=WebServerTypes.IIS6&&this.ServerType!=WebServerTypes.IIS7)
返回null;
DirectoryEntry root=this.GetDirectoryEntry(“IIS://“+this.DomainName+”/W3SVC/AppPools”);
if(root==null)
返回null;
DirectoryEntry AppPool=root.Invoke(“创建”、“IIsApplicationPool”、AppPoolName)作为DirectoryEntry;
AppPool.CommitChanges();
返回应用程序池;
}
/// 
///返回应用程序池的实例
/// 
/// 
/// 
public DirectoryEntry GetApplicationPool(字符串AppPoolName)
{
DirectoryEntry root=this.GetDirectoryEntry(“IIS://“+this.DomainName+”/W3SVC/AppPools/“+AppPoolName”);
返回根;
}
/// 
///按路径检索Adsi节点。为错误处理而抽象
/// 
///要检索的ADSI路径:IIS://localhost/w3svc/root
///节点或空值
private DirectoryEntry GetDirectoryEntry(字符串路径)
{
DirectoryEntry root=null;
尝试
{
root=新目录条目(路径);
}
抓住
{
this.SetError(“无法访问节点”);
返回null;
}
if(root==null)
{
this.SetError(“无法访问节点”);
返回null;
}
返回根;
}

简言之,有两种方法让人想到

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

C:\WINDOWS\system32\inetsrv\MetaBase.xml
您只需使用Linq2Xml并解析Xml以查找站点名称或Id,AppPoolId属性包含AppPool的名称


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

为了确保,我测试过一次,name还可以;然后,我打开IIS管理器,更改应用程序池,执行
iisreset
,然后再次运行测试应用程序-我得到的应用程序池名称再次正确。我不知道您的代码是什么样子的,但我的代码是这样的:

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;
    }
}
使用系统;
使用System.IO;
使用System.DirectoryServices;
班级
{
静态void Main(字符串[]参数)
{
DirectoryEntry=FindVirtualDirectory(“,”默认网站“,”);
if(条目!=null)
{
Console.WriteLine(entry.Properties[“AppPoolId”].Value);
}
}
静态DirectoryEntry FindVirtualDirectory(字符串服务器、字符串网站、字符串虚拟机)
{
DirectoryEntry siteEntry=null;
DirectoryEntry rootEntry=null;
尝试
{
siteEntry=FindWebSite(服务器、网站);
如果(siteEntry==null)
{
返回null;
}
rootEntry=siteEntry.Children.Find(“根”,“IIsWebVirtualDir”);
if(rootEntry==null)
{
返回null;
}
返回rootEntry.Children.Find(virtualdir,“IIsWebVirtualDir”);
}
捕获(DirectoryNotFoundException ex)
{
返回null;
}
最后
{
如果(siteEntry!=null)siteEntry.Dispose();
如果(rootEntry!=null)rootEntry.Dispose();
}
}
静态DirectoryEntry FindWebSite(字符串服务器、字符串friendlyName)
{
string path=string.Format(“IIS://{0}/W3SVC”,服务器);
使用(DirectoryEntry w3svc=新的DirectoryEntry(路径))
{
foreach(w3svc.Children中的DirectoryEntry条目)
{
如果(entry.SchemaClassName==“IIsWebServer”&&
entry.Properties[“ServerComment”].Value.Equals(friendlyName))
{
返回条目;
}
}
}
返回null;
}
}
对不起,我的英语很糟糕。

希望我能提供帮助。

我已经使用了这些方法,但除非使用相同的代码显式设置应用程序池名称,否则无法正确检索应用程序池名称。这意味着,如果您使用iis管理器手动添加网站并设置应用程序池,那么当我使用sharepoint创建应用程序并设置其他应用程序池时,这些代码将不再有效(它将始终返回DefaultAppPool)。这些方法不起作用。谢谢,我不知道。也许你应该在你的问题中添加这些信息?很好的一个,适用于IIS7和IIS6。请记住,如果您的服务器