How can I kill some active processes by searching for their .exe filenames in C# .NET or C++?
-
2If you need kill process by partial name see http://stackoverflow.com/questions/14632162/c-sharp-killing-a-process-by-a-part-of-its-name?rq=1. – Alexei Levenkov Sep 18 '14 at 15:22
9 Answers
Quick Answer:
foreach (var process in Process.GetProcessesByName("whatever"))
{
process.Kill();
}
(leave off .exe from process name)

- 24,169
- 25
- 107
- 177

- 6,639
- 3
- 32
- 51
-
4what should be do if above code return Exception (a 32 bit processes cannot access modules of a 64 bit process) ? – Manish Parmar Aug 31 '13 at 11:29
-
46Leave off ".exe". From MSDN: "The process name is a friendly name for the process, such as Outlook, that does not include the .exe extension or the path" – Jun 03 '14 at 16:50
-
-
1@AgainMe Process.Kill() sends a kill signal to a process, which will halt its execution wherever it happens to be. This is different from an interrupt signal in that the process will not have a chance to respond and/or clean up from the signal. No more execution will happen in that process, and any locks on resources used by that process will be released. Environment.Exit() is performed by the currently executing process to kill itself with a success code, which is perfectly safe. Process.Kill() is not nearly as safe as Environment.Exit(). – jchitel Feb 21 '17 at 02:51
-
2I suggest the use of LINQ: var procs = Process.GetProcesses().Where(pr => pr.ProcessName.Contains("Spotify")); – Leandro Bardelli Jul 18 '17 at 18:33
-
2Any option to kill a specific instance of a process? I mean, _`Contains("Spotify"))` kills all the instances of `Spotify`._ **I want to kill a particular instance of `Spotify`.** – Banee Ishaque K Feb 13 '18 at 05:17
-
1@BaneeIshaqueK I had a similar requirement, I had used MainWindowTitle to find the right process and used CloseMainWindow to close it. You can try it – Anto Varghese Dec 09 '19 at 09:27
My solution is to use Process.GetProcess()
for listing all the processes.
By filtering them to contain the processes I want, I can then run Process.Kill()
method to stop them:
var chromeDriverProcesses = Process.GetProcesses().
Where(pr => pr.ProcessName == "chromedriver"); // without '.exe'
foreach (var process in chromeDriverProcesses)
{
process.Kill();
}
Update:
In case if you want to do the same in an asynchronous way (using the C# 8
Async Enumerables
), check this out:
const string processName = "chromedriver"; // without '.exe'
await Process.GetProcesses()
.Where(pr => pr.ProcessName == processName)
.ToAsyncEnumerable()
.ForEachAsync(p => p.Kill());
Note: using async
methods doesn't always mean code will run faster.
The main benefit is that the foreground thread will be released while operating.

- 7,904
- 4
- 42
- 42
-
-
16Funny coincidence is, I was looking in this thread for a solution to killing the chromedriver. Must be a common issue. – kerl Aug 09 '17 at 20:08
-
1Same here. that doesn't seem to solve it though. The initial console that gets fired off is actually an instance of chrome.exe and I'm guessing you don't want to force close all of those unless its a build/test agent – Dan Csharpster May 22 '19 at 20:41
You can use Process.GetProcesses()
to get the currently running processes, then Process.Kill()
to kill a process.

- 161,458
- 45
- 265
- 341
-
7
-
1what should be do if above code return Exception (a 32 bit processes cannot access modules of a 64 bit process) ? – Manish Parmar Aug 31 '13 at 11:29
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

- 144
- 1
- 3
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();
}

- 115
- 9
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());
}
}

- 426
- 5
- 8
For my part, I wanted to be sure to catch the right process... ;-)
So I do:
private static void KillProcessesByPath(string fullExePath)
{
var processName = Path.GetFileNameWithoutExtension(fullExePath);
foreach (var process in Process.GetProcessesByName(processName))
{
if (process.MainModule?.FileName != fullExePath) continue;
process.Kill();
}
}

- 1,466
- 17
- 31
public void EndTask(string taskname)
{
string processName = taskname.Replace(".exe", "");
foreach (Process process in Process.GetProcessesByName(processName))
{
process.Kill();
}
}
//EndTask("notepad");
Summary: no matter if the name contains .exe, the process will end. You don't need to "leave off .exe from process name", It works 100%.

- 732,580
- 175
- 1,330
- 1,459

- 41
- 1
-
1a simple `.Replace(".exe", "")` on the top voted answer would do this with a lot less convoluted and unnecessary code – AndrewK Jan 12 '18 at 22:11
-
The whole idea of it is to see the method with or without .exe so people can see multiple ways of handling it... It's not meant for copy and paste.... – user7993881 Mar 09 '18 at 02:40
-
this won't work if the name actually contains ".exe" e.g. "my.exeption.exe" Alternatively you could check substring of last four characters.. although again that would fail for a name with "my.exe.exe". Just pointing it out. – Sidupac Apr 15 '21 at 02:43