I want to extend the current PATH variable with a C# program. Here I have several problems:
Using GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine)
replaces the placeholders (i.e. '%SystemRoot%system32'
is replaced by the current path 'C:Windowssystem32'
). Updating the PATH variable, I dont want to replace the placeholder with the path.
After SetEnvironmentVariable
no program can't be opened from the command box anymore (i.e. calc.exe in the command box doesn't work). Im using following code:
String oldPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable("PATH", oldPath + ";%MYDIR%", EnvironmentVariableTarget.Machine);
After editing and changing the PATH
variable with Windows everything works again. (I thing changes are required, otherwise it is not overwritten)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…