简体   繁体   中英

Check IIS Application pool if its empty (C# / IIS6)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM