I am trying to add a folder to my "Path" environment variable by doing the following:
string path = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);
path = path + @";C:\my\new\path";
Environment.SetEnvironmentVariable("Path", path, EnvironmentVariableTarget.Machine);
This adds my new folder to the Path variable successfully, however any existing Path folders that had been represented by an environment variable are now expanded out.
For example, I have an environment variable JAVA_HOME that points to C:\Program Files\Java\jdk-17, and in the Path variable I originally had an entry that read %JAVA_HOME%\bin. However, after doing the above update, now that Path entry reads C:\Program Files\Java\jdk-17\bin.
When I examine the string returned by Environment.GetEnvironmentVariable(), I see that it returns the paths already expanded out rather than the %JAVA_HOME% placeholder. How do I update the Path variable without saving the existing paths in their expanded forms and instead maintain their variable names?