【问题标题】:How to Get Site ID and site status in IIS 7 using Web Administration C#如何使用 Web 管理 C# 在 IIS 7 中获取站点 ID 和站点状态
【发布时间】:2013-10-22 14:29:20
【问题描述】:

我在使用 Web 管理获取 IIS7 上的站点 ID 和状态方面需要帮助。我有这个代码

 private string getSiteIdByName(string siteName)
    {
        DirectoryEntry root = getDirectoryEntry("IIS://" + textServer.Text + "/W3SVC");
        foreach (DirectoryEntry e in root.Children)
        {
            if (e.SchemaClassName == "IIsWebServer")
            {
                if (e.Properties["ServerComment"].Value.ToString().Equals(siteName, StringComparison.OrdinalIgnoreCase))
                {
                    return e.Name;
                }
            }
        }
        return null;
    }



  private void showStatus(string siteId)
        {
            string result = "unknown";
            DirectoryEntry root = getDirectoryEntry("IIS://" + textServer.Text + "/W3SVC/" + siteId);
            PropertyValueCollection pvc;
            pvc = root.Properties["ServerState"];
            if (pvc.Value != null)
                result = (pvc.Value.Equals((int)ObjectState.Start) ? "Running" :
                          pvc.Value.Equals((int)ObjectState.Stop) ? "Stopped" :
                          pvc.Value.Equals((int)ObjectState.Pause) ? "Paused" :
                          pvc.Value.ToString());
            labelStatus.Text = result + " (" + pvc.Value + ")";
        }

但它确实不适用于我们的某些服务器。有没有办法为此使用 Web 管理?非常感谢任何帮助。

谢谢!

【问题讨论】:

    标签: c# iis-7 web-administration


    【解决方案1】:

    稍微研究一下,您会发现您所说的 WebAdministration API 只不过是一个美化的 XML 文件管理器,IIS 竭尽全力向您隐藏它。此外,如果您在多个 Web 服务器上“反射”,API 往往会表现得很糟糕。它在后台使用 DCOM,所以那里有很多您可能并不真正感兴趣的开销。

    在过去遇到这种情况时,我学会了绕过 Web 管理,只用 LINQ to XML 破解打开的 XML 文件。一旦你开始查看文件结构,它并没有那么复杂。

    这是一个标准的.config 文件,位于C:\Windows\System32\inetsrv\config\applicationHost.config。

    如果您想跨服务器访问它,请使用 URI:

    file://myserver/c$/windows/system32/inetsrv/config/applicationHost.config
    

    请注意,这可能会出现问题,具体取决于运行您的应用程序的帐户是否有权访问您正在使用的共享。

    然后,只需使用 LINQ to XML 解析它或使用 Configuration 命名空间中的类加载它。

    顺便说一句,此配置文件包含站点 ID、虚拟目录路径、物理路径、应用程序池信息,以及您可能想了解的有关服务器上每个站点的所有信息。

    祝你好运。

    【讨论】:

      【解决方案2】:

      我绝对不建议手动解析 XML,因为要像其他人建议的那样正确解析 XML 有很多复杂性,例如加密、默认值的继承以及您可能会出错的许多其他问题。这也意味着您无法访问运行时状态,实际上您可以这样做非常简单:

      using(ServerManager serverManager = new ServerManager()) {
          var state = serverManager.Sites[siteName].State;
      }
      

      见:enter link description here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 2011-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多