检测应用程序的启动

RC1*_*140 12 c# c++ windows

在启动外部应用程序时,如何在Windows上使用C#进行检测?

我尝试了FilesystemWatcher,它不起作用,因为文件没有真正改变.还有一个计时器不断检查所有打开的进程可能有点过分杀死.有没有其他方法可以做到这一点?如果没有在C#中,可以在C++中这样做(如果是这样,请举个例子).

我想这样做的原因是为了记录目的.

Mar*_*orp 21

您可以使用System.ManagementWMI(Windows Management Instrumentation)

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);
        }
  }
Run Code Online (Sandbox Code Playgroud)