By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,346 Members | 2,331 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,346 IT Pros & Developers. It's quick & easy.

Identify user that ran process/executable

markmcgookin
Expert 100+
P: 648
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
Feb 17 '09 #1
Share this Question
Share on Google+
3 Replies


Plater
Expert 5K+
P: 7,872
? The username a process is running as is available in the taskmanager (ctrl+alt+del)
In your own application, I think it's Environment.User or something
Feb 17 '09 #2

Expert 100+
P: 750
PRR
try this code...
Expand|Select|Wrap|Line Numbers
  1. try
  2.                     {
  3.                         ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from Win32_Process where Name='notepad.exe'");
  4.  
  5.                         foreach (ManagementObject obj in searcher.Get())
  6.                         {
  7.                             Console.WriteLine(obj["Name"].ToString());
  8.  
  9.                             object[] o = new object[2];
  10.                             obj.InvokeMethod("GetOwner", o);
  11.  
  12.                             Console.WriteLine("User: " + o[0].ToString());
  13.                             Console.WriteLine("Computer Name: " + o[1].ToString());
  14.                         }
  15.                     }
  16.  
  17.  
  18.                     catch (Exception ee)
  19.                     {
  20.  
  21.                     }
  22.  
add reference to System.Management
Feb 18 '09 #3

markmcgookin
Expert 100+
P: 648
Thanks for the replies folks,

Expand|Select|Wrap|Line Numbers
  1. 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
Feb 18 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.