如何区分用户启动的流程和系统启动的流程?

时间:2010-12-29 09:03:02

标签: c# .net process

我的代码段如下: -

KillUserProcess()  
{  
  foreach (Process myProcess in Process.GetProcesses())   
   {  
      // here I need to know which is system process and which is user process:  
       like --if(myProcess.type==user)  
               myProcess.Kill();  
}  

实际上我想要停止所有用户启动的进程,但不要停止系统启动的进程。

2 个答案:

答案 0 :(得分:2)

在Bytes.com上找到:Get Process Account Name

您可以使用System.Management和Win32_Process类。

using System;
using System.Management;
using System.Diagnostics;
class App {
    public static void Main() {
    GetProcessInfo(Process.GetCurrentProcess().Handle. ToInt32());
    }

    static void GetProcessInfo(int handle)
    {
        using(ManagementObject proc = new ManagementObject("Win32_Process.Handle='" + handle.ToString() + "'"))
        {
            proc.Get();
            string[] s = new String[2];
            //Invoke the method and populate the array with the user name and domain
            proc.InvokeMethod("GetOwner",(object[])s);
            Console.WriteLine("User: " + s[1]+ "\\" + s[0]);
        }
    }
}

答案 1 :(得分:2)

要杀死大多数用户启动的进程:

 System.Diagnostics.Process.Start("shutdown -l");