Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PATH environment variable containing other environment variables

I know that I can query any environment variable by calling:

System.Environment.GetEnvironmentVariable();

Now for example, I want the value of windir, that is %SystemRoot%. However, the function interpretes it as C:\Windows (which is the value of SystemRoot).

I get the same behaviour by querying the registry key with Registry.LocalMachine.OpenSubKey()

Is there a way to get variable names from an environment variable instead of their paths? (I need it in order to easily switch between multiple installed versions of a SDK I work with.)

like image 971
kobermann Avatar asked Dec 07 '25 06:12

kobermann


1 Answers

As far as I know, the only way to do that, is to read the environment variable directly from the registry and specify that you do not want it expanded:

registryKey.GetValue("PATH", "", RegistryValueOptions.DoNotExpandEnvironmentNames);

See the docs for RegistryValueOptions.

like image 63
M-Peror Avatar answered Dec 09 '25 19:12

M-Peror