Solved

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

Posted on 2013-05-16
5
1,249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

710 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