Get Process User Name

Recently I was developing a little one-off application for use on our home computer that I share with Erin. I was working on a library that would enable you to easily add items to the shell context menu in windows, so you could put your own custom context menu options onto specific file types.

In order to test the library out, I had to register the assembly using regasm.exe, then copy the assemblies to the GAC so explorer.exe could find them, and then I needed to restart explorer.exe, so that it would refresh the code.  The problem came when I wanted to restart explorer, because we use fast user switching, so it was possible for Erin to have explorer.exe running, as well as myself, and I didn’t want to kill her instance of explorer, I wanted to kill mine. So I needed to know how to get the username of a process, because there isn’t a convenient Process.UserName property on the System.Diagnostics.Process class (don’t ask me why, I don’t get it either).

Anyways, a quick google search brought up some results, and I thought I would share them here. I found a vb solution on experts-exchange, and I ported this to c#, heres the code to get the username of a process, by process ID:


static string GetProcessOwner(int processId)
{

string query = “Select * From Win32_Process Where ProcessID = “ + processId;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();

foreach (ManagementObject obj in processList)
{
     string[] argList = new string[] { string.Empty };
     int returnVal = Convert.ToInt32(obj.InvokeMethod(“GetOwner”, argList));
     if (returnVal == 0)
         
return argList[0];
}

return “NO OWNER”;

}

Of course, I come to find out later on that its a bad idea to write shell extensions in managed code, which makes sense when you think about it.  Oh well, it was interesting work while it lasted :)

 

7 Comments so far »

  1. Anonymous said,

    Wrote on February 1, 2006 @ 7:33 pm


  2. Anonymous said,

    Wrote on March 10, 2006 @ 3:30 pm

    Keep in mind that this requires the System.Management namespace, which I don’t think is on the reference list by default.

    I’m pretty sure this has something to so with the Windows Management Interface (WMI) stuff. So if the WMI services aren’t running, this may not work at all?

  3. Looping until a user presses a key? - Page 2 - CodeCall Programming Forum said,

    Wrote on January 23, 2008 @ 10:33 pm

    […] other handles. I found an article on how to do #3, but I don’t understand it; maybe you could: Get Process User Name It’s in C#, of which I have no […]

  4. Payal Bansal said,

    Wrote on February 14, 2008 @ 4:34 am

    nice article …really helped me to solve my problem….thanks

  5. Dimitri said,

    Wrote on March 28, 2008 @ 5:08 am

    Thank You!

  6. Dave said,

    Wrote on July 18, 2008 @ 8:33 pm

    thank you i don’t know why i didn’t think of this before. ^_^
    And yes you have to add a reference for System.Management namespace.
    and yes that is a wmi query. Thanks again to the author!

    btw… You really have to go out of your way to turn off wmi… And this will also work remotely for those of you who didn’t know.

  7. Justin said,

    Wrote on March 4, 2009 @ 1:12 am

    Here’s another way, also WMI:

    GetOwner Method of the Win32_Process Class:

    http://msdn.microsoft.com/en-us/library/aa390460(VS.85).aspx

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Enter my name (ben) in this box, so I know you're a human.

Comment: