Subscribe to 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!

| More
This article was originally written on 11/23/06 Tagged with: Programming

Daily Email Updates

You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


Name:
Email:

Comments (16)

  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


Leave a Comment




Leave your friendly comment here.

If you have a computer help question, click here to leave it on the forums instead.

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

Our Friends
Getting Started


About How-To Geek
What Is That Process?
svchost.exe
jusched.exe
dwm.exe
ctfmon.exe
wmpnetwk.exe
mDNSResponder.exe
wmpnscfg.exe
rundll32.exe
wfcrun32.exe
Ipoint.exe
Itype.exe
Wfica32.exe
Mobsync.exe
conhost.exe
Dpupdchk.exe Adobe_Updater.exe

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