Solved

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

Posted on 2013-05-16
5
1,193 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
OK, thanks, I'll try that.
0
 

Expert Comment

by:OTSSolutions
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
This video discusses moving either the default database or any database to a new volume.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now