我正在使用System.Management.ManagementEventWatcher来获取已启动进程的进程ID和可执行路径:
private void startWatcher_EventArrived(Object sender, EventArrivedEventArgs e)
{
String processID = e.NewEvent.Properties["ProcessID"].Value.ToString();
var searcher = new ManagementObjectSearcher(new WqlObjectQuery(String.Format("Select ExecutablePath from Win32_Process where ProcessID = {0}", processID)));
ManagementObject managementObject = null;
foreach (ManagementObject obj in searcher.Get())
{
managementObject = obj;
break;
}
Console.WriteLine(managementObject["ExecutablePath"]);
}
使用此WQL查询:
从中选择ExecutablePath Win32_ProcessStartTrace
有没有办法可以避免进行对象搜索,但仍然使用EventArrivedEventArgs对象中已有的内容获取ExecutionPath?
我真正需要的是每个启动的新进程的ProcessID和ExecuatblePath。这是最简单的方法吗?