【问题标题】:Hot to get the status of a Windows service in ASP / .net?在 ASP / .net 中获取 Windows 服务的状态很热门?
【发布时间】:2009-01-29 02:16:44
【问题描述】:

我需要找到一种方法来通过 HTTP 监视 Windows 服务列表的状态,最好不使用任何第三方程序。

我真正需要做的就是显示服务名称及其状态(“已启动”/“已停止”)。

我不是 ASP 程序员,所以这有点超出我的范围。我已经搜索过,但还没有找到很多。

感谢任何帮助或建议。

【问题讨论】:

    标签: asp.net windows-services


    【解决方案1】:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ServiceProcess;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
    
    
            ServiceController[] services = ServiceController.GetServices();
    
            Response.Write("List of running services : <BR>");
            foreach (ServiceController service in services)
            {
    
    
                Response.Write(string.Format(" Service Name: {0} , status {1} <BR>", service.ServiceName, service.Status.ToString()));
    
    
    
            }
        }
    }
    

    记得添加system.serviceprocess引用

    【讨论】:

    • 如果网站使用 Impersonation 并且 ServiceController.GetServices() 返回访问被拒绝怎么办?网站如何访问此方法?谢谢。
    【解决方案2】:

    在我看来,您希望枚举 REMOTE 计算机上的服务。这可以使用 WMI(Windows 管理规范)来完成,方法如下:

    ConnectionOptions connection = new ConnectionOptions();
    connection.Username = userNameBox.Text;
    connection.Password = passwordBox.Text;
    connection.Authority = "ntlmdomain:DOMAIN";
    
    ManagementScope scope = new ManagementScope("\\\\FullComputerName\\root\\CIMV2", connection);
    scope.Connect();
    
    ObjectQuery query= new ObjectQuery("SELECT * FROM Win32_Service"); 
    
    ManagementObjectSearcher searcher = 
                        new ManagementObjectSearcher(scope, query);
    
    foreach (ManagementObject queryObj in searcher.Get())
    {
         Console.WriteLine("-----------------------------------");
         Console.WriteLine("Win32_Service instance");
         Console.WriteLine("-----------------------------------");
         Console.WriteLine("Caption: {0}", queryObj["Caption"]);
         Console.WriteLine("Description: {0}", queryObj["Description"]);
         Console.WriteLine("Name: {0}", queryObj["Name"]);
         Console.WriteLine("PathName: {0}", queryObj["PathName"]);
         Console.WriteLine("State: {0}", queryObj["State"]);
         Console.WriteLine("Status: {0}", queryObj["Status"]);
    }
    

    此代码直接取自here,Happy Coding!

    【讨论】:

      【解决方案3】:
      Process p = new Process();
      p.StartInfo.UseShellExecute = false;
      p.StartInfo.RedirectStandardOutput = true;
      p.StartInfo.FileName = "sc query service_name";
      p.Start();
      string output = p.StandardOutput.ReadToEnd();
      p.WaitForExit();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-22
        • 1970-01-01
        • 2017-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多