Identify user that ran process/executable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • markmcgookin
    Recognized Expert Contributor
    • Dec 2006
    • 648

    Identify user that ran process/executable

    Hi there,

    Is there any way in C# to determine which user kicked off your application? I have an app that a third party piece of software (Nokia Intellisync) kicks off, but we aren't sure which user it is doing this under, I thought it would be the intellisync user, but we are not sure and just wanted to know if there was a way of outputting the username of the user that executed an application.

    Thanks for your time folks,

    Mark
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    ? The username a process is running as is available in the taskmanager (ctrl+alt+del)
    In your own application, I think it's Environment.Use r or something

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      try this code...
      Code:
      try
                          {
                              ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from Win32_Process where Name='notepad.exe'");
      
                              foreach (ManagementObject obj in searcher.Get())
                              {
                                  Console.WriteLine(obj["Name"].ToString());
      
                                  object[] o = new object[2];
                                  obj.InvokeMethod("GetOwner", o);
                                  
                                  Console.WriteLine("User: " + o[0].ToString());
                                  Console.WriteLine("Computer Name: " + o[1].ToString());
                              }
                          }
      
      
                          catch (Exception ee)
                          {
      
                          }
      add reference to System.Manageme nt

      Comment

      • markmcgookin
        Recognized Expert Contributor
        • Dec 2006
        • 648

        #4
        Thanks for the replies folks,

        Code:
        Environment.UserName;
        Is perfect for what I needed. Thanks for the other code though, useful for finding out who is using another process.

        Cheers!

        Mark

        Comment

        Working...