远程机器上的服务检查 [英] Service check on remote machine
问题描述
大家好,
我试图检查服务是否在我的远程计算机上运行。
那说我写的一些代码可以很好地报告服务正在运行,但是当我停止服务并尝试进行检查时,该过程失败并显示异常消息Not Found。
使用(ManagementObjectSearcher searcher = new ManagementObjectSearcher(范围,SERVICE ))
{
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject service in collection)
{
if (service [ Started]。等于( true ))
{
label.Text = SERVICE:正在运行;
}
else if (service [ 已停止]。等于( true ))
{
label.Text = SERVICE:已停止;
}
else if (service [ 已暂停]。等于( true ))
{
label.Text = 服务:暂停;
}
else if (service [ 停止]。等于( true ))
{
label.Text = 服务:停止;
}
else if (service [ 开始]。等于( true ))
{
label.Text = 服务:开始;
}
else
{
label.Text = 服务:更改;
}
}
}
停止检查失败。这告诉我它确实检测到该服务确实已停止,但它无法向我显示。我做错了什么?
要在IF语句之后检查服务是否正在运行就足够了:
if (service [ 已开始]。等于( true ))
{
lbl_eid_deliver.Text = SERVICE:正在运行;
}
else
{
lbl_eid_deliver.Text = 服务:离线;
}
但是,还有一种方法可以通过使用ServiceController来做同样的事情。
可以在下面提供的链接中看到一个例子。
http://stackoverflow.com/questions/ 178147 /如何-可以-I-验证-IF-一个 - 窗口服务正在运行
Hi all,
I am attempting to check that the services are running on my remote machine.
That said I have wrote some code that works great reporting that the service is running, however when I stop the service and try to do a check, the process fails with an exception message "Not Found".
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, SERVICE))
{
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject service in collection)
{
if (service["Started"].Equals(true))
{
label.Text = "SERVICE: Running";
}
else if (service["Stopped"].Equals(true))
{
label.Text = "SERVICE: Stopped";
}
else if (service["Paused"].Equals(true))
{
label.Text = "SERVICE: Paused";
}
else if (service["Stopping"].Equals(true))
{
label.Text = "SERVICE: Stopping";
}
else if (service["Starting"].Equals(true))
{
label.Text = "SERVICE: Starting";
}
else
{
label.Text = "SERVICE: Changing";
}
}
}
It fails on "Stopped" check. That tells me that it does detect that the service really is stopped but it fails to show me that. What am I doing wrong?
To simply check that the service is running following IF statement is sufficient to do so:
if (service["Started"].Equals(true)) { lbl_eid_deliver.Text = "SERVICE: Running"; } else { lbl_eid_deliver.Text = "SERVICE: Offline"; }
However, there is a another method that can be used to do same thing by using ServiceController.
An example can be seen in the link provided below.
http://stackoverflow.com/questions/178147/how-can-i-verify-if-a-windows-service-is-running
这篇关于远程机器上的服务检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!