Retreive and Destruct a List of Active Windows and their Process Names in VB.NET

In this article we demonstrate on how to get and Destroy a list of Active windows and the process names associated with them.
  • 2786
 

This Article will make you understand that how to get a List of Active Windows and the Processes which they are associated with, how you can Destroy the selected process.
 

Following are the Steps

  • So to start the working Firstly create a new Window forms project. Than from the tool box add one List Box, two Buttons on form. You can select these control from the Toolbox as shown below.

    Active-Window1.gif

    Active-Window2.gif

     
  • Give the names to the controls as lstMain to ListBox, btnRefresh to Button1 and btnDestroy to Button2. Now your form will look like the below form.

    Active-Window3.gif
     
  • Now double click on btnReferesh button and put the below code on Button_click Event.

      Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
            lstMain.Items.Clear()
            For Each p As Process In Process.GetProcesses
               If p.MainWindowTitle = String.Empty = False Then
                    lstMain.Items.Add(p.ProcessName & " # " & p.MainWindowTitle & " # " & p.Id)
               End If
            Next
      End Sub
     
  • If you run your project the ListBox will be populated by active window names of most of the running application. by this working you will get all the active windows and associated processes with them.
     
  • if you also want to destroy any of the selected process than go on window forms double click on btnDestroy button and add the below code on Button_click Event.

    Private Sub btnDestroy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDestroy.Click
        Try
            Dim whole As String = lstMain.SelectedItem.ToString
            Dim pid = whole.Substring(whole.LastIndexOf(" # "))
            pid = pid.Replace(" # ", "")
            Dim p As Process = Process.GetProcessById(pid)
            Try
                p.Kill()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            Catch ex As Exception
                MessageBox.Show(ex.Message)
        End Try
    End Sub
 

Output

Active-Window4.gif

Active-Window6.gif

Active-Window7.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.