How to get Application Pool name through code (C#, ASP.net)

狂风中的少年 提交于 2019-12-01 15:48:06

May this can help: ApplicationPoolName Property

Namespace: Microsoft.Web.Administration Assembly: Microsoft.Web.Administration (in Microsoft.Web.Administration.dll)

http://msdn.microsoft.com/en-us/library/microsoft.web.administration.application.applicationpoolname(v=vs.90).aspx

dIvYaNsH sInGh

Modified version of @Razon answer :)

public static string GetCurrentApplicationPoolName()
    {
        ServerManager manager = new ServerManager();
        string DefaultSiteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
        Site defaultSite = manager.Sites[DefaultSiteName];
        string appVirtualPath = HttpRuntime.AppDomainAppVirtualPath;

        string appPoolName = string.Empty;
        foreach (Application app in defaultSite.Applications)
        {
            string appPath = app.Path;
            if (appPath == appVirtualPath)
            {
                appPoolName = app.ApplicationPoolName;
            }   
        }
        return appPoolName;
    }

In many cases it might be enough to just read the name of the application pool from the environment variable:

var apppool = System.Environment.GetEnvironmentVariable(
                  "APP_POOL_ID", EnvironmentVariableTarget.Process);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!