Kill a Process in C#
To kill a process in C# you will need to have processId
or ProcessName
available, First get all processes using Process.GetProcesses()
method, this method returns a list of Process
objects, then you can loop through this list and check for the process name or process id, Finally you can kill the process found using Kill()
method.Remember to include required namespace:
using System.Diagnostics;
.Example
using System;
using System.Diagnostics;
public class Program
{
public static void Main() {
Process[] runingProcess= Process.GetProcesses();
for (int i = 0; i < runingProcess.Length; i++) {
if(runingProcess[i].ProcessName == "mspaint") {
runingProcess[i].Kill();
}
if(runingProcess[i].Id == 12345) {
runingProcess[i].Kill();
}
}
}
}