在 C# 中以编程方式杀死 vista/windows 7 中的进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/544687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
programmatically kill a process in vista/windows 7 in C#
提问by mmr
I want to kill a process programmatically in vista/windows 7 (I'm not sure if there's significant problems in the implementation of the UAC between the two to make a difference).
我想在 vista/windows 7 中以编程方式杀死一个进程(我不确定两者之间的 UAC 实现是否存在重大问题以产生影响)。
Right now, my code looks like:
现在,我的代码如下所示:
if(killProcess){
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("MyProcessName");
// Before starting the new process make sure no other MyProcessName is running.
foreach (System.Diagnostics.Process p in process)
{
p.Kill();
}
myProcess = System.Diagnostics.Process.Start(psi);
}
I have to do this because I need to make sure that if the user crashes the program or exits abruptly, this secondary process is restarted when the application is restarted, or if the user wants to change the parameters for this secondary process.
我必须这样做,因为我需要确保如果用户崩溃程序或突然退出,则在重新启动应用程序时重新启动此辅助进程,或者如果用户想要更改此辅助进程的参数。
The code works fine in XP, but fails in Windows 7 (and I assume in Vista) with an 'access is denied' message. From what the Almighty Google has told me, I need to run my killing program as administrator to get around this problem, but that's just weak sauce. The other potential answer is to use LinkDemand, but I don't understand the msdn page for LinkDemand as it pertains to processes.
该代码在 XP 中运行良好,但在 Windows 7 中失败(我假设在 Vista 中)并显示“访问被拒绝”消息。根据全能的谷歌告诉我的,我需要以管理员身份运行我的杀戮程序来解决这个问题,但这只是弱项。另一个可能的答案是使用 LinkDemand,但我不了解 LinkDemand 的 msdn 页面,因为它与流程有关。
I could move the code into a thread, but that has a whole host of other difficulties inherent to it that I really don't want to discover.
我可以将代码移动到一个线程中,但这有很多其他固有的困难,我真的不想发现。
采纳答案by Andrew Ensley
You are correct in that it's because you don't have administrative priveleges. You can solve this by installing a service under the local system user and running a custom command against it as needed.
您是对的,因为您没有管理权限。您可以通过在本地系统用户下安装服务并根据需要对其运行自定义命令来解决此问题。
In your windows form app:
在您的 Windows 窗体应用程序中:
private enum SimpleServiceCustomCommands { KillProcess = 128 };
ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, "SERVICE_NAME");
scp.Assert();
System.ServiceProcess.ServiceController serviceCon = new System.ServiceProcess.ServiceController("SERVICE_NAME", Environment.MachineName);
serviceCon.ExecuteCommand((int)SimpleServiceCustomCommands.KillProcess);
myProcess = System.Diagnostics.Process.Start(psi);
In your service:
在您的服务中:
private enum SimpleServiceCustomCommands { KillProcess = 128 };
protected override void OnCustomCommand(int command)
{
switch (command)
{
case (int)SimpleServiceCustomCommands.KillProcess:
if(killProcess)
{
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("MyProcessName");
// Before starting the new process make sure no other MyProcessName is running.
foreach (System.Diagnostics.Process p in process)
{
p.Kill();
}
}
break;
default:
break;
}
}
回答by Andrew Ensley
I'll add the code for Simon Buchan's suggestion. It makes sense and should work as well, assuming your windows form is what launched the process in the first place.
我将添加 Simon Buchan 建议的代码。假设您的 Windows 窗体首先启动了该过程,这很有意义并且应该也能正常工作。
Here's where you create the process. Notice the variable myProc. That's your handle on it:
这是您创建流程的地方。注意变量 myProc。这是你的处理方式:
System.Diagnostics.Process myProc = new System.Diagnostics.Process();
myProc.EnableRaisingEvents=false;
myProc.StartInfo.FileName="PATH_TO_EXE";
myProc.Start();
Later, just kill it with:
稍后,只需使用以下命令杀死它:
myProc.Kill();