Solved

Get windows username in c# windows service in 64-bit Windows 7

Posted on 2013-05-16
5
1,221 Views
Last Modified: 2013-09-09
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<ManagementBaseObject>().First()["UserName"];

Thanks
0
Comment
Question by:pkoivula
5 Comments
 
LVL 6

Expert Comment

by:APoPhySpt
ID: 39170896
hi, have you tried:



using System.Security.Principal;
......
WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
String username = wp.Identity.Name;

Or

For UserName ->  System.Security.Principal.WindowsIdentity.GetCurrent().User

For ComputerName ->  System.Security.Principal.WindowsIdentity.GetCurrent().Name
0
 

Author Comment

by:pkoivula
ID: 39171376
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
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39173942
Most services log on with their own credentials, most often Local System or Network Service. Since they have their own logon, they do not see the "real" user.

I have searched a lot for the same question in the past, but found nothing.

I ended up creating an .exe that was installed so that it is started in the background at startup. It thus runs on the current user account and the name of the user can then be retrieved without problem.
0
 

Author Comment

by:pkoivula
ID: 39377305
OK, thanks, I'll try that.
0
 

Expert Comment

by:OTSSolutions
ID: 39466754
This code is run on 64 bit windows service but not get exact user name "NT AUTHORITY\SYSTEM". Please suggest any thing else.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question