Environment.GetEnvironmentVariable won't find variable value

后端 未结 4 1401
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 22:26

Why won\'t Environment.GetEnvironmentVariable(\"variableName\") get a variable\'s value if the call is made from within a webMethod hosted on IIS and it will wo

相关标签:
4条回答
  • 2020-12-05 23:16

    Read here for more information:

    Using System Wide Environment Variables in .NET Application


    Specifically:

    What Are System Environment Variables?

    Environment variables are strings that save information about the entire environment in your system. These string values are dynamic and they can affect the way your system will behave on. Environment variables can be classified into two main types:

    System Variables: They affect the entire system whatever the current user is. They are defined by Windows and saved in the registry. You need to be an administrator to be able to modify them. You usually need to restart your computer to make these changes effective.

    User Variables: They affect the current environment of the current system user. They can be deleted, modified, and added by any system user. They are used by Windows setup, by some programs, and by users. Changes to these variables are saved to the registry and be effective immediately.


    If you try to invoke an environment variable that does not exist on your machine you will have issues. You must be trying to find a variable that exists on your local machine, but not on your web service's host machine.

    0 讨论(0)
  • 2020-12-05 23:16

    Restarting Visual Studio fixed it for me (guessing IIS Express also caches these values).

    0 讨论(0)
  • 2020-12-05 23:16

    I faced the same issue, and thanks to sergserg's answer, I came up with this and it worked:

    var value = Environment.GetEnvironmentVariable(key, EnvironmentVariableTarget.User)
    

    The important bit was using EnvironmentVariableTarget.User

    0 讨论(0)
  • 2020-12-05 23:30

    You need to restart IIS by using the iisreset command.

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