2014-09-04 55 views
7

我想通过我的应用程序回收应用程序池。如何通过代码获取应用程序池名称(C#,ASP.net)

此前我将应用程序池名称存储在我的数据库中并使用它进行回收。 但过去发生的情况是,我们将应用程序从一个应用程序池移到另一个应用程序池,有时我们忘记更新数据库中的应用程序池名称。

所以我想通过应用程序获取应用程序池名称并将其用于回收。

+0

这样的事情? http://stackoverflow.com/questions/1400464/enumerating-application-pools-in-iis – fuchs777 2014-09-04 11:42:48

回答

12

修改版本的@Razon回答:)

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

恕我直言,更好的答案比拉索答案 – Kiquenet 2016-02-04 13:35:21

+0

结合链接代码和那应该是答案。 – Rexxo 2016-02-05 15:59:00

+0

注意:如果您的IIS上的AppPool中的“加载用户配置文件”设置未设置为“True”,则此代码可能会抛出COM异常 – 2018-02-19 23:56:24

7

在很多情况下,它可能足以只读从环境变量的应用程序池的名称:

var apppool = System.Environment.GetEnvironmentVariable(
        "APP_POOL_ID", EnvironmentVariableTarget.Process); 
相关问题