如何检测当前正在运行的应用程序池?(IIS6)

4

我需要知道如何检测当前运行的应用程序池,以便可以通过编程方式对其进行回收。

有人知道如何在IIS6中实现吗?

我目前用于回收应用程序池的代码是:

    /// <summary>
    /// Recycle an application pool
    /// </summary>
    /// <param name="IIsApplicationPool"></param>
    public static void RecycleAppPool(string IIsApplicationPool) {
        ManagementScope scope = new ManagementScope(@"\\localhost\root\MicrosoftIISv2");
        scope.Connect();
        ManagementObject appPool = new ManagementObject(scope, new ManagementPath("IIsApplicationPool.Name='W3SVC/AppPools/" + IIsApplicationPool + "'"), null);

        appPool.InvokeMethod("Recycle", null, null);
    }   
2个回答

7

经过搜索,我自己找到了答案:

   public string GetAppPoolName() {

        string AppPath = Context.Request.ServerVariables["APPL_MD_PATH"];

        AppPath = AppPath.Replace("/LM/", "IIS://localhost/");
        DirectoryEntry root = new DirectoryEntry(AppPath);
        if ((root == null)) {
            return " no object got";
        }
        string AppPoolId = (string)root.Properties["AppPoolId"].Value;
        return AppPoolId;
    }

嗯,他们需要一种让我设定自己的答案为“正确答案”的方法。


2

我也找到了这个,并且它对我起作用了。请注意,你可能需要包含使用 System.DirectoryServices 的引用;

    private static string GetCurrentApplicationPoolId()
    {
        string virtualDirPath = AppDomain.CurrentDomain.FriendlyName;
        virtualDirPath = virtualDirPath.Substring(4);
        int index = virtualDirPath.Length + 1;
        index = virtualDirPath.LastIndexOf("-", index - 1, index - 1);
        index = virtualDirPath.LastIndexOf("-", index - 1, index - 1);
        virtualDirPath = "IIS://localhost/" + virtualDirPath.Remove(index);
        DirectoryEntry virtualDirEntry = new DirectoryEntry(virtualDirPath);
        return virtualDirEntry.Properties["AppPoolId"].Value.ToString();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接