I am trying to get the current application pool name of a web application. I googled a lot and I found several ways to do it but none of them seems to work for me This is one of my implementations:
   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();
    }
I get as an error: Unknown error (0x80005000)
Control panel - program features - turn on off - look for IIS 6 compatibily and check everything.
If you have the HttpContext in place use this:
 HttpContext.Current.Request.ServerVariables["APP_POOL_ID"]
If not use this:
System.Environment.GetEnvironmentVariable(
  "APP_POOL_ID", 
  EnvironmentVariableTarget.Process);
User contributions licensed under CC BY-SA 3.0