• ARTICLES
SEARCH

How-To Geek

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!

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 11/23/06

Comments (25)

  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();

  15. CodeMunter

    Ok, so we have a list of processes, and I’m particularly interested in the WINWORD one… So how can I turn that into my private Microsoft.Office.Interop.Word.ApplicationClass _Word object so that I can pull its levers n shiz?

  16. silverback12

    it outputs the result I wanted. but at the end it throws the error

    explorer: 2656: Tuesday, October 06, 2009 1:48:15 PM
    svchost: 960: Tuesday, October 06, 2009 1:45:15 PM
    ConsoleApplication1.vshost: 5676: Thursday, October 08, 2009 10:16:22 AM
    smax4pnp: 2916: Tuesday, October 06, 2009 1:48:29 PM
    RunRM: 3860: Tuesday, October 06, 2009 1:48:38 PM
    Error: System.ComponentModel.Win32Exception: Access is denied
    at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 acces
    s, Boolean throwIfExited)
    at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfE
    xited)
    at System.Diagnostics.Process.GetProcessTimes()
    at System.Diagnostics.Process.get_StartTime()
    at ConsoleApplication1.Program.Main(String[] args) in c:\c#\ConsoleApplicatio
    n1\ConsoleApplication1\Program.cs:line 20

  17. WaQa

    I can’t get the code to work.

    This is the problematic line

    Console.WriteLine(”Process: {0}; ID: {1}”, theprocess.ProcessName, theprocess.Id);

    The IDE returns 23 errors for that line of code ranging from ; and ) expected to invalid expression term etc.

  18. WaQa

    The issue is the quotes. They need to be ” instead of ”, which look like smart quotes from a word doc, not quotes from an IDE.

  19. Dave

    The code above works great if you are running as the Admin. But is there a way to force p.StartTime and p.TotalProcessorTime to work when you are running your app as a user and not Admin? (It works on some processes but not on all).

  20. Pawan Bhise

    Good Thanks!

  21. Heinrich

    I am gettin to much errors!

  22. Balaji Birajdar

    The processs.getprocesses() returns all the currently active processes. However, I am not able to filter the processes for the currently logged in user…

    any ideas on how to do it?

  23. razeyeh razy

    i have a number of circle in form and between points have line.
    how to color input circle?
    i keep points circle with a list, for line have a list.

  24. Alice

    How can I find out the process is updated or refreshed? – I’m waiting the window done with updated data and then update another process.
    I tried process1.HasExited but this is for when the window closed, as far as the window(application) is open the process1.HasExited always = false.

  25. jyothi

    how to typecast System.Diagnostics process to an acadapplication

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!