如何杀死进程,无论它是否是32位或64位? [英] How to kill process regardless whether it's 32 or 64-bit?

查看:1400
本文介绍了如何杀死进程,无论它是否是32位或64位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我在某一点的工具,我想杀死被它的名字的过程。现在我正在测试上的Win7 64位,但我收到的错误是:

In my tool at a certain point I want to kill a process by it's name. I'm testing now on Win7 64-bit, but the error I receive is:

一个32位进程无法访问64的模块

A 32 bit processes cannot access modules of a 64 bit process

Code:

代码>工艺[] = runningProcesses Process.GetProcesses();
的foreach(在runningProcesses工艺过程)
{
//现在检查过程
的foreach的模块(在process.Modules ProcessModule模块)
{
如果(module.FileName.Equals(communicator.exe))
{
process.Kill();
}
}
}

Process[] runningProcesses = Process.GetProcesses(); foreach (Process process in runningProcesses) { // now check the modules of the process foreach (ProcessModule module in process.Modules) { if (module.FileName.Equals("communicator.exe")) { process.Kill(); } } }



我将不得不使用这种许多计算机上的工具(32/64的WinXP,Win7的32/64,Win8的64),我需要这个工具在这两个类型的架构工作。
如何实现这一目标?

I will have to use this tool on many computers (WinXP 32/64, Win7 32/64, Win8 64) and I need this tool to work on both type of architectures. How to achieve this?

推荐答案

使用的 Process.GetProcessByName() ,将在大多数情况下,等同于寻找名字(主)模块。你仍然将不得不面对的事实,这将返回多个进程。你可能不想杀死他们。情况因人而异。

Use Process.GetProcessByName(), which will in most cases by identical to looking for the name of (main) module. You will still have to deal with the fact, that this will return multiple processes. You may not want to kill all of them. YMMV.

foreach (Process process in Process.GetProcessByName("communicator"))
{
    process.Kill();
}



另外请注意,方法异步运行,也就是说,它可以返回各自的进程实际上已经被杀害之前。你可以添加一个 Process.WaitForExit()如果你的关心。

Also note that the Kill method runs asynchronously, i.e. it may return before the respective process has actually been killed. You could add a Process.WaitForExit() if you care.

这篇关于如何杀死进程,无论它是否是32位或64位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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