How to find out which user owns the software?

Ok, so I'm trying to find the name of the user for whom this process belongs.

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

        }
        Console.ReadLine();

Currently, I can find out the process identifier for each process, but not the user. Is there a way to find out who is the user who owns the process if I know the process ID?

+3
source share
2 answers

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

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

In addition, you can store owners and pid in the Dictionary for better 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();
        }
    }
}
+3
source

- WMI.

0

Source: https://habr.com/ru/post/1734486/


All Articles