Search code examples
c#windows-services

What would Environment.UserName return when used in a Windows Service


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?


Solution

  • 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