Kill some processes by .exe file name

Viewed 222214

How can I kill some active processes by searching for their .exe filenames in C# .NET or C++?

8 Answers

If you have the process ID (PID) you can kill this process as follow:

Process processToKill = Process.GetProcessById(pid);
processToKill.Kill();

You can Kill a specific instance of MS Word.

foreach (var process in Process.GetProcessesByName("WINWORD"))
{
    // Temp is a document which you need to kill.
    if (process.MainWindowTitle.Contains("Temp")) 
        process.Kill();
}

Depending on how many processes there are to kill (e.g. when its hundreds like in my case), foreaching over all of them might take quite a while. (interesting sidenote: while Kill() was usually quite quick in .NET FW 4.8 , somehow in NET 6.0 Windows its a lot slower - seeing multiple Win32Exceptions in the debug/trace until the target process is finally done)

Anyway back to topic: In case of an app shutdown, where u need to make sure every process is is gone, consider using the TAP library - particulary the Parallel shortcuts, hundreds of processes killed within a glimpse.

Usage example:

var procs = Process.GetProcessByName("mydirtyprocesses");

if (procs.Length == 0) return;

procs.AsParallel().ForAll(process => 
{
   try
   {
      process.Kill();

   // No process linked to the process comp (mostly because the process died in 
   // the short timespan between invoking GetProcess() and the effective 
   // initialization of the props/fields of the component. -OR- Process has 
   // already exited (when the exit happened after the process component has 
   // beenpopulated (difference is, in case 1 you cannot even get the Process 
   // ID from // the component, in case 2 you see data like Id and get the true 
   // for HasExited // - so always be prepared for that.
   // catch (InvalidOperationException) 
   {
     // Process is gone, no further action required
     return;
   }

   // Ensuring process is gone (otherwise try again or fail or whatever)
   if (!process.HasExited)
   { 
       // Handle it
   }
}

In this particular scenario just wrap it properly in try/catch , as with such a number of processes the probability for an exception is quite increased

static void Main()
    {
        string processName = Process.GetCurrentProcess().ProcessName;
        int processId = Process.GetCurrentProcess().Id;
        
        Process[] oProcesses = Process.GetProcessesByName(processName);

        if (oProcesses.Length > 1)
        {
            if ((MessageBox.Show("Application is opened!", "",MessageBoxButtons.YesNo) == DialogResult.Yes)) ;
            {
                foreach (var process in Process.GetProcessesByName(processName))
                {
                    if (process.Id != processId)
                    {
                        process.Kill();
                    }
                }
            }
        }
        else
        {   
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmLogin());
        }
    }
Related