pkoivula
asked on
Get windows username in c# windows service in 64-bit Windows 7
Hi,
How to get the current logged in username from C# Windows service in Windows 7 64-bit system.
I'm using the following code which works fine in 32-bit Windows:
ManagementObjectSearcher searcher = new ManagementObjectSearcher(" SELECT UserName from Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();
string username = (string)collection.Cast<Ma nagementBa seObject>( ).First()[ "UserName" ];
Thanks
How to get the current logged in username from C# Windows service in Windows 7 64-bit system.
I'm using the following code which works fine in 32-bit Windows:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("
ManagementObjectCollection
string username = (string)collection.Cast<Ma
Thanks
ASKER
Hello APoPhySpt,
Thanks for the reply but the solution you provide wont work for the Windows Service. My requirement is to get the current logged in username in which my windows service is runnng. The above code works for windows application but not windows service.
Thanks
Thanks for the reply but the solution you provide wont work for the Windows Service. My requirement is to get the current logged in username in which my windows service is runnng. The above code works for windows application but not windows service.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK, thanks, I'll try that.
This code is run on 64 bit windows service but not get exact user name "NT AUTHORITY\SYSTEM". Please suggest any thing else.
using System.Security.Principal;
......
WindowsPrincipal wp = new WindowsPrincipal(WindowsId
String username = wp.Identity.Name;
Or
For UserName -> System.Security.Principal.
For ComputerName -> System.Security.Principal.