我有简单的代码来跟踪服务状态。但在我的代码中,我只能知道服务启动或停止。我想知道哪个服务启动或停止。
这是我的代码示例。这是主要功能:
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);
}