Subscribe to How-To Geek

Recommended: Click Here to Run a Free Scan for Common PC Errors   [Sponsored Link]

Get a List of Running Processes in C#

The System.Diagnostics namespace contains functions that allow you to manage processes, threads, eventlogs and performance information.

The System.Diagnostics.Process object gives you access to functionality enabling you to manage system processes. We will use this object to get a list of running processes.

Add this line to your using list:

using System.Diagnostics;

Now you can get a list of the processes with the Process.GetProcesses() method, as seen in this example:

Process[] processlist = Process.GetProcesses();

foreach(Process theprocess in processlist){
Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
}

Some interesting properties of the Process object:

p.StartTime (Shows the time the process started)
p.TotalProcessorTime (Shows the amount of CPU time the process has taken)
p.Threads ( gives access to the collection of threads in the process)

The .NET framework really makes things simple!

The Geek is the founder of How-To Geek and a geek enthusiast. When he's not coming up with great how-to articles, he's probably writing at his personal blog. This article was written on 11/23/06 and tagged with: Programming, C#

Comments (14)

  1. Brandon

    Simple Correction: (at least to fix mine)

    Process processlist = Process.GetProcesses();

    should be:

    Process[] processlist = Process.GetProcesses();

    Using the original posting way gave 'Cannot implicitly convert' errors, due to Process.GetProcesses() returning an array.

    As a side note, make sure to remove and replace the quotes in the Console.Writeline command, if the compiler complains about that statement. The quotes copied as nonstandard quotations on my machine.

    Just trying to help others, thanks for the post, I didn't know about System.Diagnostics until stumbling upon this posting.

  2. The Geek

    Brandon,

    Thanks for catching that… typing from memory =)

    I'm not sure why, but wordpress converts the quotes to some other quote value. I'll have to look into that.

  3. Syed

    Hi.. i want to know which new process the user has requested to open.. i want to control the processes or files to be used by user in C#…

    can anyone give up how to achive this.. give the code also if u can…

  4. Umit

    Hi. I also want to know which you want to know. Please help me!! I want to control the processes which one is open or close???

  5. Khan

    I am creating a process using some arguments and once the process start I wanted to take a look at the arguments. How do I do that? I have tried using process.StartInfo.Arguments and it didn't work. Please help!!!

  6. mukesh

    is there any way to track how much time a person is working on a particular file

  7. ssp

    Hi, Is there a way to know how much CPU each thread for a process(say, w3wp.exe) uses?? In other words, w3wp process creates N no. of threads and I want to know exactly how much CPU each thread is using for this particular process.

  8. Eric

    is there any way to spit out the command line arguments that were used to fire the given process?
    likie for explorer.exe it would show"C:\WINDOWS\Explorer.EXE" and mmc.exe would show " C:\WINDOWS\system32\perfmon.msc /s"

    I can see these values using SysInternals process explorer so i'm hoping I can get them using .Net somehow. CommanArgument doesn't seem to be an available property on the Process class.

  9. Asem

    Its really a great job..I keep on wasting 3 Hrs to search what you guys are done here..
    Thanks a lot..

    Asem Ibohal

  10. Asem

    How can I get these running process and add to a collection using LINQ ?

    Say, u have a class SystemData(double,string,double,double). You can add the Process information using Linq as :

    public systemdata(): base()
    {
    //Add data here
    string P_NAME; //process name
    double P_ID; //process ID
    double P_MEM; //memory Usages
    double X_axis=1;

    var procname = from n in Process.GetProcesses()
    select n;
    foreach (var Pname in procname)
    {
    P_ID = Pname.Id;
    P_MEM = Pname.WorkingSet64 / 1024;
    P_NAME = Pname.ProcessName;
    Add(new SystemData(P_ID, P_NAME, P_MEM,X_axis));
    X_axis ++;
    }
    }
    For complete sample, You can mail me at : iamibohal@yahoo.com

  11. Shaaz

    This is great. But I do have one question.

    How would I get an event when a process is started or ended?

    Basicly, I am trying to make my own taskbar in C#.

  12. dzar

    how we can selection to choice only proceses running?

  13. ChokoMd

    Process.GetCurrentProcess();

  14. ChokoMd

    *My bad - Process[] processlist = Process.GetProcesses();


Leave a Comment




Leave your friendly comment here. If you have a computer help question, leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Copyright © 2006-2008 HowToGeek.com. All Rights Reserved.