开发者

How to read a user environment variable in C#?

开发者 https://www.devze.com 2023-01-10 08:11 出处:网络
How can I read a user specific environment variable? I know how to get a system wide one, like Environment.GetEnvironmentVariable(\"SOMETHING开发者_StackOverflow中文版\");

How can I read a user specific environment variable? I know how to get a system wide one, like

Environment.GetEnvironmentVariable("SOMETHING开发者_StackOverflow中文版");

Thanks in advance!


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.


It's the same method, just set the second parameter to be User as:

System.Environment.GetEnvironmentVariable("varName", EnvironmentVariableTarget.User);


Use second overload of GetEnvironmentVariable which let's you specify EnvironmentVariableTarget.


var UserName = Environment.GetEnvironmentVariable("username");
0

精彩评论

暂无评论...
验证码 换一张
取 消