How to kill a running process in C# and VB.NET

To kill a running process in C# and VB.NET you can use the following snippet.

Sample C#

public static void KillProcess(string name, bool waitForExit = true)
{
	foreach (var process in Process.GetProcessesByName(name))
	{
		try
		{
			process.Kill();
			if (waitForExit)
			{
				process.WaitForExit();
			}
		}
		catch (Exception ex)
		{
			//handle the exception your way
		}

	}
}

Sample VB.NET

Public Shared Sub KillProcess(name As String, Optional waitForExit As Boolean = True)
	For Each process__1 As var In Process.GetProcessesByName(name)
		Try
			process__1.Kill()
			If waitForExit Then
				process__1.WaitForExit()
			End If
				'handle the exception your way
		Catch ex As Exception

		End Try
	Next
End Sub

One thought on “How to kill a running process in C# and VB.NET

Leave a Reply