VB.NET Get Process list and kill process

39,734

Solution 1

the list + kill:

For Each p As Process In Process.GetProcesses
    ListBox1.Items.Add(p.ProcessName.ToString)
    If String.compare(p.ProcessName, "iexplore",true) = 0 Then
        p.Kill()
    End If
Next

use the kill() to kill process
to open process:

Process.Start("pname.exe")

Solution 2

You can also create a list with all running processes of Excel before starting Excel programatically (using Linq statement):

Dim lstExcelProcess As List(Of Process) = (From p As Process In Process.GetProcesses Where p.ProcessName.ToUpper Like "Excel*".ToUpper).ToList

And after you finished what you had to do, kill the only process that was not listed before:

Dim process As Process = (From p As Process In process.GetProcesses Where p.ProcessName.ToUpper Like "Excel*".ToUpper And Not lstExcelProcess.Contains(p))(0)
            process.Kill()
Share:
39,734
BlackOpty
Author by

BlackOpty

Updated on June 29, 2020

Comments

  • BlackOpty
    BlackOpty almost 4 years

    i want to insert all processes that running into listbox,
    and also how to "kill" process and start process?

    for i = 0 to procCount
    ...
    next i