在 C# 中杀死除我的程序之外的所有进程 [英] Kill all processes besides my program in C#

查看:61
本文介绍了在 C# 中杀死除我的程序之外的所有进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不,这不是恶意软件.我正在尝试制作一个 PC 助手工具,该工具可以终止所有非 Windows 进程的任务,但在此过程中,它会自行终止.

No, this is not Malware. I'm trying to make a PC helper tool that kills all tasks that are non-Windows processes, but in the process, it kills itself.

这是一个控制台应用程序,所以我尝试不包括 cmd.exe 来杀死,但它仍然杀死了我的程序.

It's a console application, so I tried disincluding cmd.exe to kill, but it still kills my program.

有这种方法吗?

if (process.ToString == this.ExecutableName)

推荐答案

没有比

Process self = Process.GetCurrentProcess() ;
foreach( Process p in Process.GetProcesses().Where( p => p.Id != self.Id ) )
{
  p.Kill() ;
}

如果你需要担心你的父进程(所以你不会杀死启动你的进程的命令外壳,问题的答案"如何获取我的应用程序的父进程的PID" 应该指导你.

If you need to worry about your parent process (so you don't kill the command shell that launched your process, the answers to the question "How can I get the PID of the parent process of my application" should guide you.

这篇关于在 C# 中杀死除我的程序之外的所有进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆