Killing a process in VB.NET

I spent some time today searching form information on how to kill a process using VB.NET. I was surprised that this was harder than I thought it would be. Mainly because the information does not seem to be documented very well.

Eventually I found some minor links which suggested some help words for searching in the VB.NET help file. There is a data type called PROCESS which can hold information about processes. In a networked environment you can play with processes on other computers. Since I am dealing with a local machine, things are easier.

The code works by getting all the processes on the local machine into an array, and then checking each of them to see if the process name is what we want. If it is we KILL the process.

Normally you would not use KILL since it does not shut things down properly. In my case I do not care since I know that there is an issue because I have already attempted to kill off the process by stopping the service.

So here is the code.

Dim local As Process() = Process.GetProcesses

Dim i As Integer

For i = 0 To local.Length – 1

Debug.WriteLine(local(i).ProcessName)

If Strings.UCase(local(i).ProcessName) = Strings.UCase(“ProcessName”) Then

local(i).Kill()

TextBox1.Text = “Found process and Killed Process”

End If

Next

3 Comments on “Killing a process in VB.NET

  1. Hi

    The code ‘Killing a process in VB.net’ is tooooo good, simple, easy to understand.

    It was really v.helpfull for me.

    Salini.

  2. This is soooo easy!! And thanx a lot for helping me!
    So if someone wants to kill multiple processes in one go try this code..just a small alteration

    Dim local As Process() = Process.GetProcesses

    Dim i As Integer

    For i = 0 To local.Length – 1
    ‘Debug.WriteLine(local(i).ProcessName)
    If Strings.UCase(local(i).ProcessName) = Strings.UCase(“notepad”) Then
    local(i).Kill()
    ElseIf Strings.UCase(local(i).ProcessName) = Strings.UCase(“winword”) Then
    local(i).Kill()
    End If
    Next

  3. hmm googd code (works)
    maybe i found your code a little too late.
    spend an hour of figuring out how to kill a process.. anyway, this is how i dun it..

    Try
    Dim xfile As String = lstProcesses.SelectedItem
    Dim xProcList As Process() = System.Diagnostics.Process.GetProcessesByName(xfile)
    For Each xproc As Process In xProcList
    xproc.Kill()
    Next
    Catch ex As Exception
    ‘exmsg here
    End Try

    lstProcesses.Refresh()

    (note that lstProcesses is a list i filled up with running processes)