我觉得似乎体验不是很好

时间:2021-12-11 07:39:19

直入主题,在网上看到关于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技术去实现,,我只是分享要领,就不过多解释了(根柢就是我本身也不懂好吧)。

参考了(剽窃):,