以编程方式确定IIS站点是否正在运行

时间:2012-02-17 18:21:43

标签: c# iis iis-7 iis-6 directoryservices

我有一个脚本可以收集IIS中的所有网站,并通过电子邮件发送一些详细信息进行审核。我想调整它,以便它只通过电子邮件发送正在运行的网站。我不需要知道停止的网站。我已经引用了IIS中的所有DirectoryEntry,但我没有看到任何可以指示它是否正在运行的属性。

这是怎么做到的?理想情况下,这应该在IIS6和IIS7上运行。

1 个答案:

答案 0 :(得分:3)

DirectoryEntry.Properties集合包含ServerState属性。它没有很好地记录,但我发现this blogger创建了他自己的枚举,似乎是正确的。枚举是

public enum ServerState
{
    Unknown = 0,
    Starting = 1,
    Started = 2,
    Stopping = 3,
    Stopped = 4,
    Pausing = 5,
    Paused = 6,
    Continuing = 7
}

使用这个,检查DirectoryEntry是否正在运行的逻辑,您将使用:

DirectoryEntry entry;
ServerState state = (ServerState)Enum.Parse(typeof(ServerState), entry.Properties["ServerState"].Value.ToString())
if (state == ServerState.Stopped || state == ServerState.Paused)
{
    //site is stopped
}
                        {
相关问题