Friday, 11 January 2013

Get a List of Running Processes in C#



Step 1)
Add  System.Diagnostics;


Step 2)   Get All Running Processes in the System.
        void GetAllRunningProcesses()
        {
            Process[] processes=System.Diagnostics.Process.GetProcesses();
           Process[] pinfo= System.Diagnostics.Process.GetProcessesByName("iisexpress");
            foreach(Process process in processes)
            {
            Console.WriteLine("Process Name:{0}, MachineName={1}",process.ProcessName,process.MachineName);
                try
                {
                System.Reflection.PropertyInfo[]pInfo=process.GetType().GetProperties();
                foreach (System.Reflection.PropertyInfo pi in pInfo)
                {
                try
                {
                Console.Write("{0}={1}",pi.Name, pi.GetValue(process, null));
                Console.WriteLine();
                }
                catch (Exception) { }
                }
                    //Console.Write(process.MachineName + "\t");
                    //Console.Write(process.ProcessName + "\t");
                    //Console.Write(process.Threads.Count + "\t");
                    //Console.Write("Days:"+process.TotalProcessorTime.TotalDays + "\t"+
                    //              "Hours:" + process.TotalProcessorTime.TotalHours + "\t"+
                    //              "MS:" + process.TotalProcessorTime.TotalMilliseconds + "\t"+
                    //              "Min:" + process.TotalProcessorTime.TotalMinutes + "\t"+
                    //              "Sec:" + process.TotalProcessorTime.TotalSeconds + "\t");
                    Console.WriteLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message+"---------------");
                }
            }
        }


Note: For each process Exceptions should be handled , Some process like "Idle" will throw Access Denied.  
So alternative approach is System.Management  has WMI management objects through which user can get Processes Information

Here is the Sample output

Process Name:sqlbrowser, MachineName=.
Properties

BasePriority=8
HasExited=False
Handle=712
HandleCount=92
Id=2688
MachineName=.
MainWindowHandle=0
MainWindowTitle=
MainModule=System.Diagnostics.ProcessModule (sqlbrowser.exe)
MaxWorkingSet=1413120
MinWorkingSet=204800
Modules=System.Diagnostics.ProcessModuleCollection
NonpagedSystemMemorySize=12648
NonpagedSystemMemorySize64=12648
PagedMemorySize=1576960
PagedMemorySize64=1576960
PagedSystemMemorySize=60472
PagedSystemMemorySize64=60472
PeakPagedMemorySize=1626112
PeakPagedMemorySize64=1626112
PeakWorkingSet=4411392
PeakWorkingSet64=4411392
PeakVirtualMemorySize=36044800
PeakVirtualMemorySize64=36044800
PriorityBoostEnabled=True
PriorityClass=Normal
PrivateMemorySize=1576960
PrivateMemorySize64=1576960
PrivilegedProcessorTime=00:00:00.0312002
ProcessName=sqlbrowser
ProcessorAffinity=3
Responding=True
SessionId=0
StartInfo=System.Diagnostics.ProcessStartInfo
StartTime=1/11/2013 5:44:02 PM
SynchronizingObject=
Threads=System.Diagnostics.ProcessThreadCollection
TotalProcessorTime=00:00:00.2184014
UserProcessorTime=00:00:00.1872012
VirtualMemorySize=35520512
VirtualMemorySize64=35520512
EnableRaisingEvents=False
WorkingSet=2150400
WorkingSet64=2150400
Site=
Container=