检测应用程序的启动 [英] Detecting the launch of a application

查看:264
本文介绍了检测应用程序的启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Windows上使用C#侦测外部应用程式启动的时机?

How do I detect with C# on Windows the moment when an external application is being launched?

我尝试使用FilesystemWatcher无法运作,因为档案不是真的改变。还有一个定时器不断检查所有打开的进程可能有点过度杀死。有没有其他方法来做到这一点?如果不是在C#中是可以这样做的C ++(如果是这样,请给我一个例子)。

I tried the FilesystemWatcher which doesn't work because the file is not really changing. Also having a timer constantly check all the open processes might be a bit over kill. Is there any other way to do this? If not in C# is it possible to do so in C++ (if so please give me an example).

我想这样做的原因是为了日志的目的。 / p>

The reason I want to do this is for logging purposes.

推荐答案

您可以使用 System.Management WMI(Windows管理规范)

class WMIEvent {
    public static void Main() {
        WMIEvent we = new WMIEvent();
        ManagementEventWatcher w= null;
        WqlEventQuery q;
        try {
    		q = new WqlEventQuery();
    		q.EventClassName = "Win32_ProcessStartTrace";
    		w = new ManagementEventWatcher(q);
    		w.EventArrived += new EventArrivedEventHandler(we.ProcessStartEventArrived);
    		w.Start();
    		Console.ReadLine(); // block main thread for test purposes
        }
    	finally {
			w.Stop();
    	}
 }

    public void ProcessStartEventArrived(object sender, EventArrivedEventArgs e) {    
    	foreach(PropertyData pd in e.NewEvent.Properties) {
    		Console.WriteLine("\n============================= =========");
    		Console.WriteLine("{0},{1},{2}",pd.Name, pd.Type, pd.Value);
    	}
  }

这篇关于检测应用程序的启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆