通过.exe文件名杀死一些进程
如何通过在 C# .NET 或 C++ 中搜索 .exe 文件名来终止某些活动进程?
How can I kill some active processes by searching for their .exe filenames in C# .NET or C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
总结:无论名称中是否包含.exe,进程都会结束。您不需要“从进程名称中删除 .exe”,它可以 100% 工作。
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%.
就我而言,我想确保抓住正确的过程......;-)
所以我这样做:
For my part, I wanted to be sure to catch the right process... ;-)
So I do:
您可以终止 MS Word 的特定实例。
You can Kill a specific instance of MS Word.
根据要杀死的进程数量(例如,像我的情况那样有数百个进程),遍历所有进程可能需要相当长的时间。 (有趣的旁注:虽然 Kill() 在 .NET FW 4.8 中通常很快,但不知怎的,在 NET 6.0 Windows 中它要慢得多 - 在调试/跟踪中看到多个 Win32Exceptions 直到目标进程最终完成)
无论如何回到主题:
如果应用程序关闭,您需要确保每个进程都消失了,请考虑使用 TAP 库 - 特别是并行快捷方式,数百个进程一目了然。
使用示例:
在这个特定场景中,只需将其正确包装在 try/catch 中,因为处理数量如此之多,发生异常的概率会大大增加
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:
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
如果您有进程 ID (
PID
),您可以按如下方式终止该进程:If you have the process ID (
PID
) you can kill this process as follow:我的解决方案是使用
Process.GetProcess()
用于列出所有进程。通过过滤它们以包含我想要的进程,我可以运行
Process.Kill()
方法来阻止它们:更新:
如果您想在异步中执行相同的操作方式(使用
C# 8
异步枚举
),请查看:注意:使用
async
方法并不总是意味着代码会运行得更快。主要好处是前台线程在运行时会被释放。
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:Update:
In case if you want to do the same in an asynchronous way (using the
C# 8
Async Enumerables
), check this out: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.
您可以使用
Process.GetProcesses()
要获取当前正在运行的进程,请使用Process。 Kill()
终止进程。You can use
Process.GetProcesses()
to get the currently running processes, thenProcess.Kill()
to kill a process.快速回答:
(从进程名称中删除 .exe)
Quick Answer:
(leave off .exe from process name)