我有简单的代码来跟踪服务状态。但在我的代码中,我可以知道服务已启动或已停止。我想知道哪个服务已启动或停止。
这是我的代码示例。 这是主要功能:
public void TrackService()
{
string queryRunning = "SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA \"Win32_Service\" AND TargetInstance.State=\"Running\"";
ManagementEventWatcher watcher = null;
watcher = new ManagementEventWatcher(queryRunning);
watcher.EventArrived += new EventArrivedEventHandler(ServiceStart);
watcher.Start();
}
这是事件功能。
private void ServiceStart(object sender, EventArrivedEventArgs e)
{
string msg = "Services has started.";
Console.WriteLine(msg);
}
答案 0 :(得分:1)
我在here搜索时发现了它。
private void ServiceStart(object sender, EventArrivedEventArgs e)
{
string msg = ((ManagementBaseObject)e.NewEvent["TargetInstance"])["DisplayName"];
}
这就是我想要的。 (ManagementBaseObject)e.NewEvent["TargetInstance"])
。