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
' 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