views:

17

answers:

2

how to get logged in user and machine name from window service in c#. when i try Environment and other class to get logged in user name it just returns NT AUTHORITY\SYSTEM from window service.

please suggest

thanks arvind

A: 

Just change the service property "Logon" (or Login or Account? I don't have a english Windows here) in Administrative tools\Services to the user that you wish. You must grant "Login as service" to this user in the local polices. Restart the service.

afurlanet
This is not what the OP was asking for. His code is in a service and he wants to know what user is currently logged in at the console.
slugster
+1  A: 

The service executes under the SYSTEM account, so that what you see in the Environment class. The machine name should not be a problem (see Gmoliv's comment). Services execute independently from whoever may be logged on: that's one of the main reasons to have them.

If you want to find out what users (yes, there may be more than one) may be logged on to your computer, you'll have to use raw Windows API's AFAIK. If you really want this, one way could be to iterate through desktops, open the named desktop, get the associated user of each desktop, and look up the account name of the user (which returns the account name on the local machine). If you only want the user which may see something on screen, use OpenInputDesktop to get a handle instead of iterating through all of them.

Note that this requires your service to have higher access rights than usual. I'd be a bit suspicious of such a service myself.

Pontus Gagge