Reputation: 157
how to get logged in user name from window service. when i do the same thing using Environment.UserName i get username as "System".
thanks Arvind
Upvotes: 1
Views: 957
Reputation: 55479
Check this link which talks about getting current loggedin user from window service -
http://www.pcreview.co.uk/forums/thread-3460184.php
Taken from above link itself -
There can be 0 or more current logged in users. If you want all of the 0 or 1 users who are currently logged in at the console then P/Invoke to WTSGetActiveConsoleSessionId and WTSQuerySessionInformation. If you want all of the users then you'll need to call WTSQuerySessionInformation in a loop.
Upvotes: 1
Reputation: 1420
You get System because your service runs under the System account.
There is an article on codeproject that discusses the same thing .. check it out http://www.codeproject.com/KB/vb/Windows_Service.aspx
System.Diagnostics.Process[] objArrProcess = System.Diagnostics.Process.GetProcessesByName("explorer");
string strCurrentUserName = objArrProcess[0].StartInfo.EnvironmentVariables["username"];
This works for me.
Upvotes: 2