I have simple codes to track service state. But in my code, i can just know service started or stopped. I want to know which service started or stopped.
Here is my code samples. This is main function:
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();
}
This is event func.
private void ServiceStart(object sender, EventArrivedEventArgs e)
{
string msg = "Services has started.";
Console.WriteLine(msg);
}