问题
How can I read a user specific environment variable? I know how to get a system wide one, like
Environment.GetEnvironmentVariable("SOMETHING");
Thanks in advance!
回答1:
Use the other overload of the Environment.GetEnvironmentVariable Method that lets you specify the EnvironmentVariableTarget.
Environment.GetEnvironmentVariable(variable, target);
target can be:EnvironmentVariableTarget.Process
,EnvironmentVariableTarget.User
,EnvironmentVariableTarget.Machine
.
回答2:
It's the same method, just set the second parameter to be User
as:
System.Environment.GetEnvironmentVariable("varName", EnvironmentVariableTarget.User);
回答3:
Use second overload of GetEnvironmentVariable which let's you specify EnvironmentVariableTarget.
回答4:
var UserName = Environment.GetEnvironmentVariable("username");
来源:https://stackoverflow.com/questions/3403895/how-to-read-a-user-environment-variable-in-c