检查IIS应用程序池是否为空(C#/ IIS6)

时间:2010-06-09 07:54:03

标签: c# .net iis wmi

我们正在构建一个可以通过配置文件安装应用程序的安装工具。涉及到很多IIS,我们大多使用WMI来完成这项工作。最近我们发现其中2个应用程序使用相同的网站和应用程序池。我可以检查网站,看它是否包含任何VDirs并删除它(卸载时)如果它是空的。现在我想对应用程序池做同样的事情:只要它包含我想跳过卸载任务的任何网站,让其他应用程序的卸载处理它。

是否可以查看应用程序池中是否有任何网站?我想知道的是它是否为空。

谢谢

1 个答案:

答案 0 :(得分:1)

我认为以下代码可以为您提供帮助:

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

当然,您可以根据需要调整行为。