Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use the <a href="http://msdn.microsoft.com/en-us/library/aa394372%28VS.85%29.aspx" rel="nofollow noreferrer">Win32_Process</a> class from the WMI to get all the info related to a process.</p> <p>check this sample </p> <pre><code>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(); } } } </code></pre> <h2>UPDATE</h2> <p>also you can store in a Dictionary the owners and the pid, for improve the performance.</p> <pre><code>using System; using System.Diagnostics; using System.Management; using System.Text; using System.Collections.Generic; namespace ConsoleApplication2 { class Program { public static Dictionary&lt;int, string&gt; GetAllProcessOwners() { Dictionary&lt;int, string&gt; d = new Dictionary&lt;int, string&gt;(); //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&lt;int, string&gt; 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(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload