【问题标题】:Check to see if an internal web site is up and running检查内部网站是否已启动并正在运行
【发布时间】:2026-01-29 03:20:10
【问题描述】:

下午,

我需要创建一个基本上列出我公司内部网站的网页。我基本上需要检查一个状态屏幕,该屏幕显示 Web 应用程序是否已启动并正在运行,或者它是否已失败。我还需要对与之关联的数据库实例执行相同的操作。

我正在寻找一些信息来帮助我在我的 ASP.net 2010 站点中编写一些 VB 代码,这些代码将为我完成对每个网站的检查。

我不是 100% 确定如何完成这项任务,但我认为我可以 Ping 应用程序名称或它所在的网络框?

我一直在查看以下 site 和代码示例以获得一些灵感,但我似乎没有进一步的进展。

是否有可能有人建议最好的前进方式并提出一些可能对我有进一步帮助的建议?

提前非常感谢。 贝蒂

【问题讨论】:

    标签: asp.net vb.net visual-studio-2010 web-applications


    【解决方案1】:

    我看到你使用了这项技术。网络微软。而且我认为您的网站和应用程序是使用“IIS”托管的?

    您应该能够使用“DirectoryEntry”类及其多个属性。 您可以像这样,检查托管在“ISS”上的网站或网络应用程序的状态,并检查应用程序池的状态。

    要使用的命名空间:System.DirectoryServices(在 C#ASP 上可用)

    有关“DirectoryEntry”及其属性的更多信息:DirectoryEntry Info

    类似这样的:

    //Get the webSite ID
    public static int GetWebsiteID(string websiteName)
    {
         string machineName = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
         DirectoryEntry w3svc = new DirectoryEntry("IIS://"+machineName+"/w3svc");
         int id = 1;
         foreach(DirectoryEntry de in w3svc.Children)
         {
              if(de.SchemaClassName == "IIsWebServer" && de.Properties["ServerComment"][0].ToString() == websiteName)
               {
                    id = int.Parse(de.Name);
               }
    
         }
         return id;
    }
    
    //Check statut of webSite
    public void checkStatutWebSite()
    {
         DirectoryEntry myWebSite = new DirectoryEntry(string.Format("IIS://" + machineName + "/w3svc/{0}/Root", GetWebsiteID("webSiteName")));
         ServerState state = (ServerState)Enum.Parse(typeof(ServerState), myWebSite.Properties["ServerState"].Value.ToString())
         if (state == ServerState.Stopped || state == ServerState.Paused)
         {
             //site is stopped
         }
    
    }
    
    public enum ServerState
    {
        Unknown = 0,
        Starting = 1,
        Started = 2,
        Stopping = 3,
        Stopped = 4,
        Pausing = 5,
        Paused = 6,
        Continuing = 7
    }
    

    希望对你有所帮助。

    【讨论】:

    • 非常感谢上述内容。是的,我们使用 IIS,并会查看 System.DirectoryServices,看看我是否可以在 VB 中实现上述操作。