MattsBits
MattsBits

MattsBits

Listing Running Processes In VB.NET  

by Matt Hawkins, 08/02/2012
Categories : Microsoft .NET

It is sometimes useful within a VB.NET program to list currently running processes in a Windows environment. This code will allow you to list running processes along with any relevant window titles.

It uses a form named "Form1" with a button named "Button1" and a list box named "ListBox1". On clicking Button1 it retrieves the list of processes into an array and then adds each item in that array to a List box.

It should be easy to modify this code to search the array for a known process name and use it to confirm if a process is running.


Public Class Form1

  Private Sub Button1_Click(ByVal sender As System.Object,
                            ByVal e As System.EventArgs) Handles Button1.Click

    ' Define array to hold list of processes
    Dim ProcessRunning() As Process

    ' Define variable to hold single process
    Dim MyProcess As Process

    ' Get list of running processes into array
    ProcessRunning = Process.GetProcesses

    ' Loop through array
    For index = 0 To ProcessRunning.Length - 1

      ' Get process from array
      MyProcess = ProcessRunning(index)

      ' Add process name to list box
      ListBox1.Items.Add(MyProcess.ProcessName)

    Next

  End Sub

End Class


Another useful attribute of the MyProcess object is MainWindowTitle. MainWindowTitle will give you the title of the window associated with the process (e.g. "Inbox - Microsoft Outlook". If a process has no open window then the MainWindowTitle attribute is empty.

Changing the FOR loop to this :
' Loop through array
For index = 0 To ProcessRunning.Length - 1

  ' Get process from array
  MyProcess = ProcessRunning(index)

  ' Check if the process has a window
  If MyProcess.MainWindowTitle <> "" Then
    ' Add process name to list box with Window title
    ListBox1.Items.Add(MyProcess.ProcessName & " (" & MyProcess.MainWindowTitle & ")")
  Else
    ' Add process name to list box
    ListBox1.Items.Add(MyProcess.ProcessName)
  End If

Next


will display the window title in brackets next to the process name if a window is associated with the process.

Author : Matt Hawkins  Last Edit By : Matt Hawkins
PHP Powered  MySQL Powered  Valid XHTML 1.0  Valid CSS  Firefox - Take Back The Web  EUKHost - Recommended Webhosting Solutions

MattHawkins CMS v3.0 - Copyright 2009-2017