Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

c# - Detecting the launch of a application

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

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).

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use System.Management and WMI (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("
============================= =========");
            Console.WriteLine("{0},{1},{2}",pd.Name, pd.Type, pd.Value);
        }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...