4

I have a virtual directory name. For this virtual directory i have to find out the application pool associated. Once i get the application pool i have to find out all the virtual directories on this application pool.. I am using this code to find out the application pool associated with virtual directory

string AppPoolName = string.Empty;
            ServerManager manager = new ServerManager();
            foreach (Site site in manager.Sites)
            {
                foreach (Application app in site.Applications)
                {
                    string path = app.Path;
                    path = path.Replace("/", " ");
                    path = path.Trim();

                    if (path.ToLower() == VDName.ToLower())
                    {
                        AppPoolName = app.ApplicationPoolName;
                        break;
                    }
                }
            }
vinay
  • 1,004
  • 1
  • 11
  • 27

3 Answers3

5
using (var serverManager = new ServerManager())
{
    var apps = (from site in serverManager.Sites
                from app in site.Applications
                where app.ApplicationPoolName.Equals("DefaultAppPool")
                select app);
}
Rajat Sharma
  • 51
  • 1
  • 1
  • 2
    Welcome to Stack Overflow! Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. (This post was flagged by at least one user, presumably because they thought an answer without explanation should be deleted.) – Nathan Tuggy Apr 14 '15 at 01:05
  • 1
    is it posible using ***appcmd*** ? `%windir%\system32\inetsrv\appcmd list apppool` ... – Kiquenet Jan 26 '18 at 10:02
  • it is possible to list all apps with appcmd, @Kiquenet, but you pointed me out to the right direction, thank you! – Eugenio Miró Jun 03 '20 at 22:13
2

I think we have to rerun the function for application pool to get the name for applications associated.

 ServerManager manager = new ServerManager();
        foreach (Site site in manager.Sites)
        {
            foreach (Application app in site.Applications)
            {

                if (app.ApplicationPoolName.ToString() == AppPoolName)
                {
                     string appname = app.Path;
                }
            }
        }
vinay
  • 1,004
  • 1
  • 11
  • 27
1

Or a new line no looping approach:

 Environment.GetEnvironmentVariable("APP_POOL_ID", EnvironmentVariableTarget.Process);
Arrya Regan
  • 1,104
  • 8
  • 22