Kill some processes by .exe file name
Solution 1
Quick Answer:
foreach (var process in Process.GetProcessesByName("whatever"))
{
process.Kill();
}
(leave off .exe from process name)
Solution 2
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.
Solution 3
You can use Process.GetProcesses()
to get the currently running processes, then Process.Kill()
to kill a process.
Solution 4
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();
}
Solution 5
If you have the process ID (PID
) you can kill this process as follow:
Process processToKill = Process.GetProcessById(pid);
processToKill.Kill();
Related videos on Youtube
Aliasghar Yaghoobzadeh
Updated on February 28, 2022Comments
-
Aliasghar Yaghoobzadeh about 2 years
How can I kill some active processes by searching for their .exe filenames in C# .NET or C++?
-
Alexei Levenkov over 9 yearsIf you need kill process by partial name see stackoverflow.com/questions/14632162/….
-
-
ConsultUtah almost 14 yearsProcess.GetProcessesByName would simplify this.
-
Manish over 10 yearswhat should be do if above code return Exception (a 32 bit processes cannot access modules of a 64 bit process) ?
-
Manish over 10 yearswhat should be do if above code return Exception (a 32 bit processes cannot access modules of a 64 bit process) ?
-
Admin almost 10 yearsLeave 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"
-
AgainMe over 7 yearsIs
Kill
safe asEnvironment.Exit(0)
? -
jchitel about 7 years@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().
-
Leandro Bardelli almost 7 yearsI suggest the use of LINQ: var procs = Process.GetProcesses().Where(pr => pr.ProcessName.Contains("Spotify"));
-
Leandro Bardelli almost 7 yearsyou could use Contains instead of equal
-
kerl over 6 yearsFunny coincidence is, I was looking in this thread for a solution to killing the chromedriver. Must be a common issue.
-
AndrewK over 6 yearsa simple
.Replace(".exe", "")
on the top voted answer would do this with a lot less convoluted and unnecessary code -
Banee Ishaque K about 6 yearsAny option to kill a specific instance of a process? I mean,
Contains("Spotify"))
kills all the instances ofSpotify
. I want to kill a particular instance ofSpotify
. -
user7993881 about 6 yearsThe 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....
-
Dan Csharpster almost 5 yearsSame 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
-
Anto Varghese over 4 years@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
-
Sidupac about 3 yearsthis 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.