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

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as