如何通过在 C# .NET 或 C++ 中搜索它们的 .exe 文件名来杀死一些活动进程?
8 回答
快速回答:
foreach (var process in Process.GetProcessesByName("whatever"))
{
process.Kill();
}
(从进程名称中去掉 .exe)
我的解决方案是Process.GetProcess()
用于列出所有进程。
通过过滤它们以包含我想要的进程,然后我可以运行Process.Kill()
方法来停止它们:
var chromeDriverProcesses = Process.GetProcesses().
Where(pr => pr.ProcessName == "chromedriver"); // without '.exe'
foreach (var process in chromeDriverProcesses)
{
process.Kill();
}
更新:
如果您想以异步方式(使用C# 8
Async Enumerables
)执行相同操作,请查看:
const string processName = "chromedriver"; // without '.exe'
await Process.GetProcesses()
.Where(pr => pr.ProcessName == processName)
.ToAsyncEnumerable()
.ForEachAsync(p => p.Kill());
注意:使用async
方法并不总是意味着代码会运行得更快。
主要好处是前台线程将在运行时被释放。
您可以使用Process.GetProcesses()
来获取当前正在运行的进程,然后Process.Kill()
杀死一个进程。
如果您有进程 ID ( PID
),您可以按如下方式终止此进程:
Process processToKill = Process.GetProcessById(pid);
processToKill.Kill();
您可以杀死 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();
}
根据要杀死的进程数量(例如,在我的情况下有数百个进程),遍历所有进程可能需要相当长的时间。(有趣的旁注:虽然 Kill() 在 .NET FW 4.8 中通常非常快,但在 NET 6.0 Windows 中它的速度要慢得多 - 在调试/跟踪中看到多个 Win32Exceptions,直到最终完成目标进程)
无论如何回到主题:在应用程序关闭的情况下,您需要确保每个进程都已消失,请考虑使用 TAP 库 - 特别是 Parallel 快捷方式,一目了然地杀死了数百个进程。
使用示例:
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
}
}
在这种特殊情况下,只需将其正确包装在 try/catch 中,因为有如此多的进程,异常的可能性会大大增加
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());
}
}
public void EndTask(string taskname)
{
string processName = taskname.Replace(".exe", "");
foreach (Process process in Process.GetProcessesByName(processName))
{
process.Kill();
}
}
//EndTask("notepad");
摘要:无论名称中是否包含.exe,进程都会结束。您不需要“从进程名称中删除 .exe”,它可以 100% 工作。