How can I kill some active processes by searching for their .exe filenames in C# .NET or C++?
ConsultUtah
Quick Answer:
foreach (var process in Process.GetProcessesByName("whatever"))
{
process.Kill();
}
(leave off .exe from process name)
My solution is:
var chromeDriverProcesses = Process.GetProcesses().
Where(pr => pr.ProcessName == "chromedriver");
foreach (var process in chromeDriverProcesses)
{
process.Kill();
}
You can use Process.GetProcesses() to get the currently running processes, then Process.Kill() to kill a process.
tomloprod
If you have the process ID (PID) you can kill this process as follow:
Process processToKill = Process.GetProcessById(pid);
processToKill.Kill();
user7993881
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%.
来源:https://stackoverflow.com/questions/3345363/kill-some-processes-by-exe-file-name