下面的代碼創建一個WQL事件查詢來檢測一個新的進程,等待一個新的進程來啓動,然後顯示有關進程的信息:
WqlEventQuery query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 1), "TargetInstance isa \"Win32_Process\"");
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
ManagementBaseObject e = watcher.WaitForNextEvent();
Console.WriteLine("Process {0} has been created, path is: {1}",
((ManagementBaseObject)e["TargetInstance"])["Name"],
((ManagementBaseObject)e["TargetInstance"])["ExecutablePath"]);
watcher.Stop();
我想你可能會找到一個解決您的通過檢查此WMI代碼示例來解決問題。希望能幫助到你。
爲什麼Windows應用程序需要回寫到某個控制檯窗口?爲什麼不只是讓Windows應用程序彈出一個非模態窗體並將輸出放在那裏呢?你怎麼能保證Windows應用程序甚至從cmd.exe窗口啓動? – 2010-04-22 12:57:19
父進程? – leppie 2010-04-22 12:57:30
重複:http://stackoverflow.com/questions/394816/how-to-get-parent-process-in-net-in-managed-way – 2010-04-22 13:20:23