If I'm not wrong, window services are started when the computer boots. So, if I try to get Environment.UserName
before a user has logged in, will it return something or will it throw an exception? If it is the former, what would it return?
Asked
Active
Viewed 241 times
-1

Vulc4n
- 59
- 6
-
What happened when you tried this? – Chetan Nov 26 '21 at 23:50
-
In general, Services don't load a user profile. Whether or no a user has logged on, the Service won't see "a user" unless it goes looking more seriously than just looking at `Environment.UserName` – Flydog57 Nov 26 '21 at 23:54
-
Even if a/multiple user(s) is logged on, a service calling Environment.Username won't get the logged on user, it gets the username of the account that runs the service – Caius Jard Nov 27 '21 at 00:02
1 Answers
2
Anything running in Windows is executing as some account, and services are no different -- which account in specific depends on how the service was started and configured.
See here for more info: https://learn.microsoft.com/en-us/windows/win32/services/service-user-accounts

Davipb
- 151
- 5
-
But, Services (generally) don't load a user profile. You can see the token (which may just be _Network Service_), but little else – Flydog57 Nov 26 '21 at 23:54
-
1@Flydog57 According to the linked Microsoft documentation: "The service control manager (SCM) automatically loads the user profile." – Davipb Nov 26 '21 at 23:56
-
-