Find a Running Process in .NET

Disclaimer: The sample code below is for educational purposes only. Do not rub on skin.

A few years back I wrote a class module in VB6 to allow an application to detect whether a specified process is currently running on the user's system. This required a lot of API code, and, while it gets the job done, it's a bit unwieldly, especially if you're debugging an app that uses it and you end up stepping through it's code rather than stepping over it. But there's a much easier way to do it in .NET, with only a few lines of code.

The answer lies in the System.Diagnostics namespace, and the Process class contained within. Why they put the Process class in the Diagnostics namespace is beyond me -- sure, you can use it for diagnostics, but also for, among other things, executing processes. But for this example, we'll focus simply on determining if a specified process is running.

First, you'll need to add a reference to the aforementioned System.Diagnostics namespace. Then add a function where you can create an array of Process objects, and initialize them by way of the static Process.GetProcessesByName() function. Check out the code below:

private void btnCheckForProcess_Click(object sender, System.EventArgs e)
   Process[] processes = null;

   this.Cursor = Cursors.WaitCursor;
   processes = Process.GetProcessesByName("winlogon");
   this.Cursor = Cursors.Default;

   if( processes.Length > 0 )
      MessageBox.Show("winlogon StartTime = " + processes[0].StartTime.ToString());
      MessageBox.Show("Process not found!");

   processes = null;

The example above declares an array of Process objects, then initializes it as described by using the static Process.GetProcessesByName() function. In this case, we are looking for the winlogon process. This can take a couple of seconds, so I set the cursor settings appropriately around the function call so that the user would know that something's going on and the program isn't just chillin'. If the process is found, the app displays it's start time, otherwise it displays a message stating that the process was not found. There should only be one instance of winlogon running, but in other cases you may end up finding more than one instance of the process you're looking for. And even though in this example we're displaying the process' start time, the Process class has many useful properties relating to the process. As I mentioned early, you can use the Process class for, among other things, starting a process, as well as other things you'll probably find useful.


Anonymous said…
Thanks for the nice article.I have a question here,can we find a IEXPLORE process based on which URL it is running. For exaple there are 3 web pages currently open on my desktop , one of them is how to programatically find this process.


Popular posts from this blog

A Generic Method Using HttpClient to Make a Synchronous Get Request

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception

How To Mock Out Child Components In Unit Tests of Angular 2 Code