Friday, 11 January 2013

Get a List of Running Processes in VB.NET

Get a List of Running Processes in VB.NET



Step 1)
Add  System.Diagnostics;


Step 2)   Get All Running Processes in the System.
 
Private  Sub GetAllRunningProcesses()
            Dim processes() As Process = System.Diagnostics.Process.GetProcesses()
           Dim pinfo() As Process =  System.Diagnostics.Process.GetProcessesByName("iisexpress")
            Dim process As Process
            For Each process In processes
Console.WriteLine("Process Name:{0}, MachineName={1}",process.ProcessName,process.MachineName)
                Try
                System.Reflection.PropertyInfo()pInfo=process.GetType().GetProperties()
                Dim pi As System.Reflection.PropertyInfo
                For Each pi In pInfo
                Try
                Console.Write("{0}={1}",pi.Name, pi.GetValue(process, Nothing))
                Console.WriteLine()
                End Try
                Next
                    '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 ex As Exception
                    Console.WriteLine(ex.Message+"---------------")
                End Try
            Next
 End Sub
      
 
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=
 
tags:Process Info in Vb.NET, Complete Process Info using VB.NET, List processes running in windows machine