Search

Kill a process using c#

The following class is a process manager, that helps to find and kill process with Id or name.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace killercodes_in.ProcKiller
{
//Process Killer
class IProcKiller
{
public void I_KillByName(string ProcName)
{
try
{
Process[] processByName = Process.GetProcessesByName(ProcName);
//Process pogo = Process.GetProcesses();
foreach (Process pr in processByName)
{
pr.Kill();
}
}
catch
{
Console.WriteLine("Cannot terminate " + ProcName + "\n");
}
}
public void I_KillByWinName(string WindowName)
{
//bool res;
try
{
Process[] pro = Process.GetProcesses();
foreach (Process p in pro)
{
if (p.MainWindowTitle.Equals(WindowName))
{
p.Kill();;
}
else
{
}
}
}
catch
{
System.Windows.Forms.MessageBox.Show("Cannot terminate " + WindowName + "\n Process Not found", "Erraor:");
}
//return true;
}
}
}