Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    }   
like image 676
Wolf5 Avatar asked Jan 23 '23 23:01

Wolf5


2 Answers

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.

like image 188
Wolf5 Avatar answered Mar 16 '23 01:03

Wolf5


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();
    }
like image 25
Jesse Alexander Romero Avatar answered Mar 16 '23 00:03

Jesse Alexander Romero