直入主题,在网上看到关于c#进程监控大多都是用遍历一次进程然后用if去判断的方法,我觉得似乎体验不是很好,这几天写第一个软件正好需要实时监控进程的模块,在网上找到了很不错的方法,整理一下分享出来给大家。
1 private void Watcher_NewProcessCreated() 2 { 3 EventQuery query = new EventQuery(); 4 query.QueryString = "SELECT * FROM" + 5 " __InstanceCreationEvent WITHIN 1 " + 6 "WHERE TargetInstance isa ‘Win32_Process‘"; 7 8 ManagementEventWatcher watcher = 9 new ManagementEventWatcher(query); 10 11 12 watcher.EventArrived += NewProcess_Created; 13 14 watcher.Start(); 15 } 16 private void NewProcess_Created(object sender, EventArrivedEventArgs se) 17 { 18 ManagementBaseObject e = se.NewEvent; 19 string Processname = ((ManagementBaseObject)e["TargetInstance"])["Name"].ToString(); 20 string ExecutablePath = ((ManagementBaseObject)e["TargetInstance"])["ExecutablePath"].ToString(); 21 22 Debug.WriteLine("进程创建:"+Processname+",进程文件路径:"+ExecutablePath); 23 }
怎么使用我想就不用多说了,这里使用了wmi技术去实现,我只是分享方法,就不过多解释了(根本就是我自己也不懂好吧)。
参考了(抄袭):http://blog.chinaunix.net/uid-108863-id-112154.html,http://blog.csdn.net/breaksoftware/article/details/50543906