C#设置环境变量 [英] C# set environment variable
问题描述
我有问题,使用C#设置环境变量。
I have problem with setting environment variables using C#.
我需要修改某些情况下,一些环境变量。例如,我需要修改NDSRC变量。
I need to modify some environment variables on some circumstances. For example I need to modify NDSRC variable.
我使用的:
Environment.SetEnvironmentVariable("MY_VARIABLE", "value", EnvironmentVariableTarget.Machine);
这工作得很好。
接下来我运行一些脚本加入WTO要求使用变量。现在有一个问题,因为脚本没有看到变量。
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);
打开Windows命令行(开始 - >运行 - > CMD.EXE)。
Open windows command line (Start->run->cmd.exe).
在命令行中键入cmd
In command line type cmd
系统无法找到cmd.exe的:CMD不被识别为一个内部或外部命令,可操作的程序或批处理文件
The system can not find cmd.exe: 'cmd' is not recognized as an internal or external command, operable program or batch file.
如果您检查Windows设置 - >环境变量,路径是否正确设置为新值。如果您在打开命令提示符检查的同时,还设置。
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.
推荐答案
不幸的是,你需要重新启动过程之前的环境变量可以被刷新。看到这个<一个href=\"http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/acf2d0f3-143e-4ba5-acdc-76a70a5c9830\"相对=nofollow> MSDN帖子。
Unfortunately, you need to restart your process before environment variables can be refreshed. See this MSDN post.
这篇关于C#设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!