Write a console utility to list processes

Write a console utility to list processes

The Process class provides all you need to create a command-line utility that lists all the processes running on the system and all the related information. This utility is therefore similar to the Windows Task Manager, except you can run it from the command prompt. All processes are sorted alphabetically.

The following routine uses the routines FormatValue and FormatMemorySize, that are available elsewhere in our Code Bank. You should compile it as a console application with the name “Processes.”

Imports System.DiagnosticsModule Module1    Sub Main()        ' get all running processes        Dim procs() As Process = Process.GetProcesses        ' create a parallel array of process names        Dim procNames(procs.Length - 1) As String        Dim i As Integer        For i = 0 To procNames.Length - 1            procNames(i) = GetProcessName(procs(i))        Next        ' sort the two arrays        Array.Sort(procNames, procs)        Console.WriteLine("PROCESS NAME___________PID__STARTED_CPU " _            & "TIME___MEMORY_THREADS_HANDLES____")        Console.WriteLine()        For i = 0 To procs.Length - 1            Dim sb As New System.Text.StringBuilder()            Dim p As Process = procs(i)            Try                ' the process name is already in the procNames array                sb.Append(FormatValue(procNames(i), 20, _                    FormatColumnAlignment.Left))                sb.Append(" "c)                ' the process ID                sb.Append(FormatValue(p.Id, 5, FormatColumnAlignment.Right))                sb.Append(" "c)                ' start time                sb.Append(FormatValue(p.StartTime.TimeOfDay, 8, _                    FormatColumnAlignment.Left))                sb.Append(" "c)                ' the CPU time                sb.Append(FormatValue(p.TotalProcessorTime, 8, _                    FormatColumnAlignment.Left))                sb.Append(" "c)                ' memory usage                sb.Append(FormatValue(FormatMemorySize(p.WorkingSet, _                    FormatMemorySizeUnits.Kilobytes), 8, _                    FormatColumnAlignment.Right))                sb.Append(" "c)                ' number of threads                sb.Append(FormatValue(p.Threads.Count, 7, _                    FormatColumnAlignment.Right))                sb.Append(" "c)                ' number of handles                sb.Append(FormatValue(p.HandleCount, 7, _                    FormatColumnAlignment.Right))                sb.Append(" "c)                Console.WriteLine(sb.ToString)            Catch ex As Exception                ' ignore exceptions            End Try        Next    End Sub    ' get the name of a process    Function GetProcessName(ByVal p As Process) As String        Try            ' the process name            Dim procName As String = p.ProcessName            If Not p.MainModule Is Nothing Then                procName = p.MainModule.ModuleName            End If            Return procName        Catch ex As Exception            ' ignore exceptions        End Try    End FunctionEnd Module


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist