0
我有簡單的代碼來跟蹤服務狀態。但在我的代碼中,我只能知道服務已啓動或已停止。我想知道哪些服務已啓動或停止。如何知道這個__InstanceModificationEvent事件觸發了哪個服務?
這是我的代碼示例。 這是主要功能:
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);
}