使用c#的窗口中没有设置环境。我哪里错了? [英] Environment is not being set in windows using c#. Where am I going wrong?
问题描述
string path = System.Environment.GetEnvironmentVariable("Path");
Console.WriteLine(path);
if (!path.Contains("C:\ccstg"))
{
if (!path.EndsWith(";"))
path = path + ';';
var v=@"C:\ccstg;";
path = path + v;
Environment.SetEnvironmentVariable("Path",path);
Console.WriteLine(path);
Console.WriteLine("Path Set");
Console.ReadKey();
}
我正在尝试使用c#设置路径环境变量,我可以得到路径,但在设置它时未设置。它也不显示任何错误。我已经尝试以管理员身份运行,没有帮助。
I am trying to set path environment variable using c#, I am able to get "Path" but while setting it is not being set. It doesn't show any error either. I have tried running it as administrator also , no help.
有没有人在这里缺少什么?
Does anybody what am I missing here ?
推荐答案
首先,您需要更加谨慎的使用字符串文字,因为\c不是有效的字符串文字转义序列,所以您发布的代码将无法编译。要修复:
Firstly, you need to be a little more careful with your string literals, the code you posted won't compile because "\c" is not a valid string literal escape sequence. To fix:
string newPathComponent = @"C:\ccstg";
if (!path.Contains(newPathComponent))
{
if (!path.EndsWith(";"))
path = path + ';';
path = path + newPathComponent;
Environment.SetEnvironmentVariable("Path", path);
现在,此代码可以在进程 >。如果要永久设置路径,则需要使用 Environment.SetEnvironmentVariable方法(String,String,EnvironmentVariableTarget)
,例如:
Now, this code works and sets the path for the duration of the process. If you want to set the path permanently, you need to use Environment.SetEnvironmentVariable Method (String, String, EnvironmentVariableTarget)
, for instance:
var target = EnvironmentVariableTarget.User; // Or EnvironmentVariableTarget.Machine
Environment.SetEnvironmentVariable("Path", path, target);
更多这里。
但是,如果你这样做,你必须小心地将您的路径组件添加到与 EnvironmentVariableTarget
相关联的路径中。这是因为%PATH%
环境变量实际上是从几个来源。如果您不小心,可以将组合路径复制到只有 EnvironmentVariableTarget.Machine
或 EnvironmentVariableTarget.User
源 - 你不想做的。
However, if you do that, you have to be careful to add your path component only to the path associated with that EnvironmentVariableTarget
. That's because the %PATH%
environment variable is actually combined from several sources. If you aren't careful, you may copy the combined path into just the EnvironmentVariableTarget.Machine
or EnvironmentVariableTarget.User
source -- which you do not want to do.
因此:
static void AddToEnvironmentPath(string pathComponent, EnvironmentVariableTarget target)
{
string targetPath = System.Environment.GetEnvironmentVariable("Path", target) ?? string.Empty;
if (!string.IsNullOrEmpty(targetPath) && !targetPath.EndsWith(";"))
targetPath = targetPath + ';';
targetPath = targetPath + pathComponent;
Environment.SetEnvironmentVariable("Path", targetPath, target);
}
最后,如果你在 Visual Studio主机进程进行调试,我观察到,如果您使用 Environment.SetEnvironmentVariable (路径,路径,EnvironmentVariableTarget.User)
,永久环境的更改将不会被选中,直到您退出并重新启动visual studio。与视觉工作室托管过程有些奇怪,我估计。为了处理这个奇怪的情况,你可能需要同时执行以下操作:
Finally, if you are running inside the Visual Studio Hosting Process for debugging, I have observed that if you use Environment.SetEnvironmentVariable("Path",path, EnvironmentVariableTarget.User)
, changes to the permanent environment will not be picked up until you exit and restart visual studio. Something weird to do with the visual studio hosting process, I reckon. To handle this odd scenario you might want to do both:
AddToEnvironmentPath(@"C:\ccstg", EnvironmentVariableTarget.User)
AddToEnvironmentPath(@"C:\ccstg", EnvironmentVariableTarget.Process)
这篇关于使用c#的窗口中没有设置环境。我哪里错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!