如何以编程方式找出用户拥有哪些进程?

时间:2010-02-25 23:56:45

标签: c#

好的,我正在尝试做的是找出给定进程所属的用户名。

        Process[] processList = Process.GetProcesses();
        foreach (Process p in processList)
        {
            Console.WriteLine(p.Id);

        }
        Console.ReadLine();

目前,我可以找到每个进程的进程ID,但不能找到用户。如果我知道进程ID,有没有办法告诉用户谁拥有该进程?

2 个答案:

答案 0 :(得分:3)

您可以使用WMI中的Win32_Process类来获取与流程相关的所有信息。

检查此示例

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Management;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {

        public static string GetProcessOwner(int PID, out string User)
        {
            string DummyStr = String.Empty;
            User = String.Empty;
            string ProcessStr = String.Empty;
            try
            {
                ObjectQuery WMIQuery = new ObjectQuery(string.Format("Select * from Win32_Process Where ProcessID ={0}", PID.ToString()));
                ManagementObjectSearcher WMIResult = new ManagementObjectSearcher(WMIQuery);
                if (WMIResult.Get().Count == 0) return DummyStr;
                foreach (ManagementObject oItem in WMIResult.Get())
                {
                    string[] List = new String[2];
                    oItem.InvokeMethod("GetOwner", (object[])List);
                    ProcessStr = (string)oItem["Name"];
                    User = List[0];
                    if (User == null) User = String.Empty;
                    string[] StrSID = new String[1];
                    oItem.InvokeMethod("GetOwnerSid", (object[])StrSID);
                    DummyStr = StrSID[0];
                    return DummyStr;
                }
            }
            catch
            {
                return DummyStr;
            }
            return DummyStr;
        }

        static void Main(string[] args)
        {
          string User;

            Process[] processList = Process.GetProcesses();
            foreach (Process p in processList)
            {
                GetProcessOwner(p.Id,out User);
                Console.WriteLine(p.Id.ToString()+' '+User);

            }
            Console.ReadLine();



        }
    }
}

更新

你也可以在字典中存储所有者和pid,以提高性能。

using System;
using System.Diagnostics;
using System.Management;
using System.Text;
using System.Collections.Generic;


namespace ConsoleApplication2
{
    class Program
    {

        public static Dictionary<int, string> GetAllProcessOwners()
        {
            Dictionary<int, string> d = new Dictionary<int, string>();
            //string DummyStr = String.Empty;
            string User = String.Empty;
            string ProcessStr = String.Empty;
            try
            {
                ObjectQuery WMIQuery = new ObjectQuery("Select * from Win32_Process");
                ManagementObjectSearcher WMIResult = new ManagementObjectSearcher(WMIQuery);
                if (WMIResult.Get().Count == 0) return d;
                foreach (ManagementObject oItem in WMIResult.Get())
                {
                    string[] List = new String[2];
                    oItem.InvokeMethod("GetOwner", (object[])List);
                    ProcessStr = (string)oItem["Name"];
                    User = List[0];
                    if (User == null) User = String.Empty;
                    //string[] StrSID = new String[1];
                    //oItem.InvokeMethod("GetOwnerSid", (object[])StrSID);
                    //DummyStr = StrSID[0];
                    d.Add(Convert.ToInt32(oItem["ProcessId"]), User);
                }
            }
            catch (Exception E)
            {
                Console.WriteLine(E.Message);
                return d;
            }
            return d;
        }


        static void Main(string[] args)
        {
            Dictionary<int, string> List = GetAllProcessOwners();
            Process[] processList = Process.GetProcesses();
            foreach (Process p in processList)
            {                                
                if (List.ContainsKey(p.Id))
                {
                    Console.WriteLine(p.Id.ToString() + ' ' + List[p.Id]);
                }                
            }
        Console.ReadLine();
        }
    }
}

答案 1 :(得分:0)

Here是一种通过WMI获取用户的方式。