Kill & Restart a Process/Program C#
using System.Diagnostics;
static bool IsProcessOpen(string name)
{
Process[] pArry = Process.GetProcesses();
foreach (Process p in pArry)
{
string s = p.ProcessName;
s = s.ToLower();
if (s.CompareTo(name.ToLower()) == 0)
{
p.Kill();
if (name == “processname”) //processname is the name of the running process to check
{
StartProcess(); //function to start process
}
return true;
}
}
return false;
}
static void StartProcess()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @”****.exe”; //path/process name
Process.Start(startInfo);
}
Advertisement
s.CompareTo(name.ToLower()) == 0
can be written as
s.Equals(name.ToLower())