tags:

views:

106

answers:

1

Hi,

We're building an installation tool which can install applications by a configuration file. There is a lot of IIS involved and we mostly use WMI for the job. Recently we discovered that 2 of the applications use the same website and application pool. I can check the website to see if it contains any VDirs and delete it (while uninstalling) if its empty. Now I want to do the same thing with the application pool: as long as it contains any websites I want to skip the uninstallationtask and let the other application's uninstall handle it.

Is it possible to see if there are any websites in an application pool? All I want to know is if its empty or not.

Thank you

+1  A: 

I think the following code can help you:

  static bool AppPoolIsShared(string appPoolName)
  {
     DirectoryEntry appPool = new DirectoryEntry(string.Format("IIS://localhost/w3svc/AppPools/{0}", appPoolName));
     if (appPool != null)
     {
        try
        {
           object[] appsInPool = (object[])appPool.Invoke("EnumAppsInPool", null);
           if (appsInPool != null && appsInPool.Length > 1)
           {
              return true;
           }
        }
        catch
        {
           return true;
        }
     }

     return false;
  }

Of course, you can adjust the behavior to your needs.

Yan Sklyarenko