Environment.GetEnvironmentVariable将找不到变量值 [英] Environment.GetEnvironmentVariable won't find variable value

查看:571
本文介绍了Environment.GetEnvironmentVariable将找不到变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why Environment.GetEnvironmentVariable("variableName") won't get 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?

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

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

实际上非常简单:

<code>

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

</code>

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

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

提前致谢

推荐答案

在此阅读更多信息:

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

具体来说:


什么是系统环境变量?

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:

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

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.

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

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.






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


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.

这篇关于Environment.GetEnvironmentVariable将找不到变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆