My program is started from a service that runs under the Local System Account
(a real user is logged in). One of the tasks of the program is store files on a network path, which should contain the current username e.g. \\server\\storage\\%username%
, but the problem is that I get the name of the system account instead of the user account when I read the environment variable:
Environment.GetEnvironmentVariable("username");
Is there a way to get the correct username in this case?
If you're not taking any measures to launch your program as a different user (CreateProcessAsUser et al.) then it's going to run as the same user as the calling program.
My solution was to find out which user started the explorer process:
Will only work if you reference the .NET
System.Management
library:Another possibility is to parse the output of the
qwinsta
command.