ManagementObject Get Owner example

!MUST ADD REFERENCE TO SYSTEM.DIAGNOSTICS EVEN IF U CAN DO USING!
static List<string> GetProcessOwner(string processName)
{
     
List<string> owners = new List<string>();
      string query = "Select * from Win32_Process Where Name = \"" + processName + "\"";
      ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
      ManagementObjectCollection processList = searcher.Get();
      foreach (ManagementObject obj in processList)
     {
           string[] argList = new string[] { string.Empty, string.Empty };
           int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
           if (returnVal == 0)
          {
                   // return DOMAIN\user
                   string owner = argList[1] + "\\" + argList[0];
                   owners.Add(owner);
          }
     }
     
return owners;
} //end GetProcessOwner

Comments

Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

Asp.Net Ending Response options, Response.End() vs CompleteRequest()

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem