How to read a user environment variable in C#?

后端 未结 4 2026
北荒
北荒 2021-02-12 12:56

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

Environment.GetEnvironmentVariable(\"SOMETHING\");
相关标签:
4条回答
  • 2021-02-12 13:30

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

    0 讨论(0)
  • 2021-02-12 13:35
    var UserName = Environment.GetEnvironmentVariable("username");
    
    0 讨论(0)
  • 2021-02-12 13:39

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

    System.Environment.GetEnvironmentVariable("varName", EnvironmentVariableTarget.User);
    
    0 讨论(0)
  • 2021-02-12 13:47

    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.

    0 讨论(0)
提交回复
热议问题