1

Thanks to the posts like this and this, I am able to get the PATH env variable and modify it as I please.

However, once I call System.Environment.GetEnvironmentVariable(name, scope), the returned string no longer has the PATH variable with %PathVariableName% (such as %SystemDrive%).

Instead of expected %SystemDrive%\folder, I get C:\folder.

While it functions as intended, I prefer to retain the variables if I can.

Is there any way to keep those path variable name when I call GetEnvironmentVariable?

Shintaro Takechi
  • 1,215
  • 1
  • 17
  • 39
  • 1
    https://stackoverflow.com/a/9845159/920069 – Retired Ninja May 07 '21 at 21:18
  • Thank you very much for the answer! I am debating if I should close my question as it asks specific question on top of simple Environment Variable change. I will answer my own question and I will let community decide if it is appropriate. – Shintaro Takechi May 07 '21 at 21:25

1 Answers1

1

Thanks to @RetiredNinja, answer was found in here answered by Nathan Bedford.

Basically the path is available in registry, so retrieving the string from there is going to allow me to keep the path variable.

string keyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment\";
string existingPathFolderVariable = (string)Registry.LocalMachine.OpenSubKey(keyName).GetValue("PATH", "", RegistryValueOptions.DoNotExpandEnvironmentNames);
Shintaro Takechi
  • 1,215
  • 1
  • 17
  • 39