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

前端 未结 3 1819
终归单人心
终归单人心 2020-12-20 15:38

I want to recycle the application pool through my application.

Previously I was storing the application pool name in my database and using that to recycle. But It ha

3条回答
  •  醉酒成梦
    2020-12-20 16:21

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

提交回复
热议问题