-1

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?

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 Answers1

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