C#设置环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9774358/
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-09 08:48:33  来源:igfitidea点击:

C# set environment variable

c#environment-variables

提问by Pablo notPicasso

I have problem with setting environment variables using C#.

我在使用 C# 设置环境变量时遇到问题。

I need to modify some environment variables on some circumstances. For example I need to modify NDSRC variable.

我需要在某些情况下修改一些环境变量。例如我需要修改 NDSRC 变量。

I use:

我用:

Environment.SetEnvironmentVariable("MY_VARIABLE", "value", EnvironmentVariableTarget.Machine);

This works fine.

这工作正常。

Next i run some script whitch uses the variable. And now there is a problem, because script does not see the variable.

接下来我运行一些使用该变量的脚本。现在有一个问题,因为脚本没有看到变量。

Example: Set Path variable (add a directory at the end) using

示例:设置路径变量(在最后添加一个目录)使用

string path = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine) + ";c:\";
Environment.SetEnvironmentVariable("Path", path, EnvironmentVariableTarget.Machine);

Open windows command line (Start->run->cmd.exe).

打开windows命令行(开始->运行->cmd.exe)。

In command line type cmd

在命令行输入cmd

The system can not find cmd.exe: 'cmd' is not recognized as an internal or external command, operable program or batch file.

系统找不到 cmd.exe: 'cmd' 不是内部或外部命令,也不是可运行的程序或批处理文件。

If you check Windows settings - > Environment Variables, Path is correctly set to new value. If you check in opened command prompt, it is also set.

如果您检查 Windows 设置 - > 环境变量,路径将正确设置为新值。如果您签入打开的命令提示符,它也会被设置。

回答by Steve Danner

Unfortunately, you need to restart your process before environment variables can be refreshed. See this MSDN post.

不幸的是,您需要在刷新环境变量之前重新启动进程。请参阅此MSDN 帖子

回答by Roman Starkov

It is by design that the variables are inherited when the process starts, and remain fixed after that.

按照设计,变量在进程开始时被继承,之后保持固定。

However there is no reason why you can't just go in to read the relevant registry keys periodically and update your process' environment variables manually from that. In fact, this is the right thing to doif you're after up-to-date values.

但是,没有理由不定期读取相关的注册表项并从中手动更新进程的环境变量。事实上,如果您追求最新的值,这是正确的做法

Basically, the registry stores a templatefor environment variables, and that's what you edit via "Windows settings - > Environment Variables". When you do that, Windows broadcasts a message to all interested parties. Any such parties can then re-create their copy of the environment variables from the registry.

基本上,注册表存储环境变量的模板,这就是您通过“Windows 设置 - > 环境变量”编辑的内容。当您这样做时,Windows 会向所有相关方广播一条消息。然后,任何此类方都可以从注册表重新创建他们的环境变量副本。

I am not aware of any ready-made function that you can just call to perform this re-creation, however, so you'll probably have to write your own.

但是,我不知道您可以调用任何现成的函数来执行此重新创建,因此您可能必须编写自己的函数。