C# Environment.GetEnvironmentVariable 找不到变量值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12754884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:19:58  来源:igfitidea点击:

Environment.GetEnvironmentVariable won't find variable value

c#.netweb-servicesiisenvironment-variables

提问by Lemmerich

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 work if I call it from a console application on the same machine?

Environment.GetEnvironmentVariable("variableName")如果调用是从托管在 IIS 上的 webMethod 中进行的,为什么不会获得变量的值,而如果我从同一台机器上的控制台应用程序调用它,它会起作用吗?

Where do I set up those variables to be visible to IIS web services? Should I use the second parameter from Environment.GetEnvironmentVariable(name, target)to get it?

我在哪里设置这些变量对 IIS Web 服务可见?我应该使用第二个参数 fromEnvironment.GetEnvironmentVariable(name, target)来获取它吗?

It is actually really simple:

其实很简单:

[WebMethod(Description = "Gets the environment variable value.")]
public string GetEnvironmentVariable()
{
    return Environment.GetEnvironmentVariable("VARIABLE_NAME_HERE");
}

And by the way, VARIABLE_NAME_HERE is set at the system and user level.

顺便说一下,VARIABLE_NAME_HERE 是在系统和用户级别设置的。

采纳答案by sergserg

Read here for more information:

阅读此处了解更多信息:

Using System Wide Environment Variables in .NET Application

在 .NET 应用程序中使用系统范围的环境变量



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.

什么是系统环境变量?

环境变量是字符串,用于保存有关系统中整个环境的信息。这些字符串值是动态的,它们会影响系统的行为方式。环境变量可以分为两种主要类型:

系统变量:无论当前用户是什么,它们都会影响整个系统。它们由 Windows 定义并保存在注册表中。您需要成为管理员才能修改它们。您通常需要重新启动计算机才能使这些更改生效。

用户变量:它们影响当前系统用户的当前环境。任何系统用户都可以删除、修改和添加它们。它们由 Windows 安装程序、某些程序和用户使用。对这些变量的更改将保存到注册表中并立即生效。



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.

如果您尝试调用机器上不存在的环境变量,则会遇到问题。您必须尝试查找本地计算机上存在的变量,但 Web 服务的主机上不存在该变量。

回答by haishan

You need to restart IIS by using the iisresetcommand.

您需要使用该iisreset命令重新启动 IIS 。

回答by Patrick Michalina

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

我遇到了同样的问题,感谢sergserg 的回答,我想出了这个并且它有效:

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

The important bit was using EnvironmentVariableTarget.User

重要的一点是使用 EnvironmentVariableTarget.User

回答by Branden Barber

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

重新启动 Visual Studio 为我修复了它(猜测 IIS Express 也会缓存这些值)。