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

发布于 2024-10-09 17:04:15 字数 334 浏览 1 评论 0原文

我的代码片段是这样的:-

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

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

My code snippet is like this:-

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

actually i want to stop all user-initiated processes but not system-initiated processes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我也只是我 2024-10-16 17:04:15

要终止大多数用户启动的进程:

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

To kill most user initiated processes:

 System.Diagnostics.Process.Start("shutdown -l");
戏蝶舞 2024-10-16 17:04:15

在 Bytes.com 上找到:获取进程帐户名称

您可以使用 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]);
        }
    }
}

Found here on Bytes.com : Get Process Account Name.

You can use System.Management and the Win32_Process class.

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]);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文