编程杀死在Vista / Windows 7中的C#过程 [英] programmatically kill a process in vista/windows 7 in C#

查看:138
本文介绍了编程杀死在Vista / Windows 7中的C#过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Vista / Windows 7的编程方式杀死一个进程(我不知道是否有在UAC两者之间实施有所作为显著的问题)。

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).

现在,我的代码如下:

  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.

代码工作在XP中正常,但在Windows 7中失败(我在Vista中承担)与访问被拒绝的消息。从什么全能谷歌已经告诉我,我需要运行杀害我的程序作为管理员来解决这个问题,但是这只是弱酱油。另一个潜在的答案是使用的LinkDemand,但因为它涉及到的过程,我不明白MSDN页的LinkDemand。

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.

我可以将代码移植到一个线程,但有先天它,我真的不想去发现其他困难一大堆。

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.

推荐答案

您是正确的在那是因为你没有管理priveleges。你可以通过在本地系统用户安装服务,并针对它运行自定义命令,需要解决这个问题。

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.

在您的Windows窗体应用程序:

In your windows form app:

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);

在你的服务:

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;
    }
}

这篇关于编程杀死在Vista / Windows 7中的C#过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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