Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-05-16
5
Medium Priority
?
1,352 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 1500 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

772 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