简体   繁体   中英

How to detect a process start & end using c# in windows?

I have a good working experience with C# but now I want to develop a simple(may be a console app) software which just detects the name and time of the process started or ended on my computer.

For example (I am assuming that my small app is already running) if a user opens Firefox then it should just insert firefox.exe into a database with time and if a user close it then it also do the same.

And same as above if a user open notepad then it should insert notepad.exe with time and so on.

I know how to insert values in database but I just need your help in identifying when the process/program starts or ends on my system.

Honestly saying I never developed this kind of application before so i have no idea that it can be possible using console app or i need to make a windows service app etc.

So please provide your answer just considering me as a beginner.

In the part of C# I am able to understand it so need to worry about that.

I am using visual studio 2010 with .net 4.0.

To do this without polling requires WMI. This is well supported in .net and you can use the ManagementEventWatcher class to subscribe to WMI notifications.

This Code Project article illustrates how it is done. Here's an extract showing how straightforward it is.

notePad = new ProcessInfo("notepad.exe");
notePad.Started +=
    new Win32Process.ProcessInfo.StartedEventHandler(this.NotepadStarted);
notePad.Terminated +=
    new Win32Process.ProcessInfo.TerminatedEventHandler(this.NotepadTerminated);

Note that ProcessInfo is a class implemented in the code attached to that article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM