0

If I call this from within a WindowsService:

var userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

I will get something along these lines back since the Service runs at a System level:

C:\Windows\system32\config\systemprofile\

What I am interested in is the user profile of the currently logged in user.

C:\Users\username\

Ideas?

konrad
  • 3,544
  • 4
  • 36
  • 75
  • https://stackoverflow.com/questions/42950323/c-sharp-windows-service-use-hkey-users-current-logged-user?rq=1 or you can call this method `System.Security.Principal.WindowsIdentity.GetCurrent().Name;` also do a quick google search there are tons of articles online about how to get current users – MethodMan Jul 10 '18 at 16:26
  • If I call `System.Security.Principal.WindowsIdentity.GetCurrent().Name` I will get back `NetworkName\System` so that's not going to work. I will have a look at the other one. – konrad Jul 10 '18 at 16:32
  • where in the process are you capturing the current user..? is the windows service actually being invoked by the individual end user.. or is this a service account that triggers the windows service..? there are several ways to do this but I would need to know how the windows service is being called.. also are you familiar with Active Directory along with PrincipalContext..? this is why I am asking the prior questions – MethodMan Jul 10 '18 at 16:35

1 Answers1

1

In general, services do not load a user profile. You can load it (https://learn.microsoft.com/en-us/windows/desktop/api/userenv/nf-userenv-loaduserprofilew), but that's generally frowned upon.

You say you are "interested in is the user profile of the currently logged in user." That's not really what the title of your post implies.

When a service is running, there may be 0, 1 or more than one currently logged in users. Getting the list of users is described in this previous post:

How to get list of all logged in users using

But remember, there may be 0, 1 or more than one result.

Flydog57
  • 6,851
  • 2
  • 17
  • 18