开发者

How do I find out what user owns what process programmatically?

Okay, so what I'm attempting to do is find out the name of the user for which a given process belongs to.

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

        }
        Console.ReadLine();

Currently, I can find out the process ID of each process, but not the user. Is there a way to 开发者_StackOverflowtell who the user is that owns the process if I know the process ID?


You can use the Win32_Process class from the WMI to get all the info related to a process.

check this sample

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



        }
    }
}

UPDATE

also you can store in a Dictionary the owners and the pid, for improve the performance.

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


Here is a way of getting the user via WMI.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜