Search code examples
c#iis-6application-pool

How to detect what Application Pool I am currently running under? (IIS6)


I need to know how I can detect the current application pool I am running under, so I can do a Recycle on it programmatically.

Does anyone know how to do this for IIS6?

My current code for recycling the app-pool is:

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

Solution

  • And after searching I found the answer myself:

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

    Hmm. They need a way to let me set my own answer as THE answer.