ARTICLE

Get and Destroy a List of Active Windows and associated Process Names in VB.NET

Posted by Shalini Juneja Articles | ASP.NET using VB.NET April 08, 2011
In this article we demonstarte on how to get and Destroy a list of Active windows and their associated Processes.
Reader Level:


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

Following are the Steps:-

  • So to start the working Firstly create a new Windowforms project. Than from the tool box add one ListBox, 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 windowforms 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

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor