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);
    }   
                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.
I found this one as well and it worked for me. Note you might need to include a reference for using 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();
    }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With