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

标签 c# iis application-pool

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

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

最佳答案

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

为了确定,我测试过一次,name name 没问题;然后,我弹出 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;
    }
}

对不起我糟糕的英语。
希望我有所帮助。

关于c# - 如何以编程方式获取特定网站 IIS6 的应用程序池名称? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/511263/

相关文章:

c# - 使用隐藏的站点地图节点来控制访问

c# - System.Data.DataTable、DataRowCollection 和 DataColumns : How does the indexing work?

asp.net - SignalR-client在短时间内发送大量请求

c# - 将动态添加的文本框中的数据添加到数据库

c# - Azure AD API 请求 401 未经授权

windows - docker :manifest for microsoft/windowsservercore:latest not found

iis - 如何查看 IIS 服务器的跟踪日志?

powershell - 使用强制选项在Powershell中创建应用程序池

asp.net - 将应用程序池空闲超时设置为 0 副作用

.net - 服务不可用 - 应用程序池无法启动