I have a custom installer action that updates the PATH environment, and creates an additional environment variable. Appending a directory to the existing path variable is working fine, but for some reason my attempts to create a new environment variable have been unsuccessful. The code I am using is:
using (RegistryKey reg = Registry.LocalMachine.OpenSubKey(@'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', true)) { reg.SetValue('MYVAR', 'SomeVal', RegistryValueKind.ExpandString); }
Edit: The OS is 32-bit XP, and as far as I can tell it is failing silently.
Is there any reason that you have to do it through the registry?
If not, you can use Environment.SetEnvironmentVariable() since .NET 2.0. It allows you to set on a machine, process or user basis.