0

我有简单的代码来跟踪服务状态。但在我的代码中,我只能知道服务启动或停止。我想知道哪个服务启动或停止。

这是我的代码示例。这是主要功能:

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);
    }
4

1 回答 1

1

我在这里 搜索时找到了它。

private void ServiceStart(object sender, EventArrivedEventArgs e)
        {
            string msg = ((ManagementBaseObject)e.NewEvent["TargetInstance"])["DisplayName"];
        }

这就是我要的。(ManagementBaseObject)e.NewEvent["TargetInstance"]).

于 2012-12-05T12:10:19.763 回答