我需要找到一种方式来监控一组 Windows 服务的状态,并通过 HTTP 显示,最好不需要任何第三方程序。
我只需要能够显示服务名称和其状态(“已启动”/“已停止”)。
我不是 ASP 程序员,所以这有点超出了我的领域。我已经搜索过了,但还没有找到太多信息。
非常感谢您的任何帮助或建议。
我需要找到一种方式来监控一组 Windows 服务的状态,并通过 HTTP 显示,最好不需要任何第三方程序。
我只需要能够显示服务名称和其状态(“已启动”/“已停止”)。
我不是 ASP 程序员,所以这有点超出了我的领域。我已经搜索过了,但还没有找到太多信息。
非常感谢您的任何帮助或建议。
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 引用
我认为您希望在远程计算机上列出服务。这可以使用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"]);
}
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();