How to get application pool name through code (C #, ASP.net)

I want to recycle an application pool through my application.

I used to store the name of the application pool in my database and use it for reuse. But in the past it happened that we moved applications from one application pool to another, and sometimes we forget to update the name of the application pool in the database.

So, I'm going to get the name of the application pool through the application and use it for recycling.

+7
c # iis application-pool
source share
3 answers

Can this help: Property ApplicationPoolName

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

+5
source share

Changed version of @Razon's 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; } 
+12
source share

In many cases, it can be quite simple to read the application pool name from the environment variable:

 var apppool = System.Environment.GetEnvironmentVariable( "APP_POOL_ID", EnvironmentVariableTarget.Process); 
+7
source share

All Articles