<p>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:</p>

<pre><code>        using (RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true))
        {
            reg.SetValue("MYVAR", "SomeVal", RegistryValueKind.ExpandString);
        }
</code></pre>

<p>Edit: The OS is 32-bit XP, and as far as I can tell it is failing silently.</p>

[{'answer_id': 40864, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 1, 'selected': False, 'text': '<p>What OS is this? Is it on a 64-bit system?  What is the nature of the failure: silent or is an exception thrown?</p>\n\n<p>You could try running <a href="http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx" rel="nofollow noreferrer">ProcessMonitor</a> and seeing if it sees the attempt to set the value.</p>\n'}, {'answer_id': 40866, 'author': 'Peter Hession', 'author_id': 441, 'author_profile': 'https://Stackoverflow.com/users/441', 'pm_score': 2, 'selected': True, 'text': '<p>Is there any reason that you have to do it through the registry?</p>\n\n<p>If not, you can use Environment.SetEnvironmentVariable() since .NET 2.0. It allows you to set on a machine, process or user basis.</p>\n'}, {'answer_id': 41952, 'author': 'Brian Stewart', 'author_id': 3114, 'author_profile': 'https://Stackoverflow.com/users/3114', 'pm_score': 0, 'selected': False, 'text': '<p>It turns out there was another problem that was preventing the code in my question from being called. However, I was using the Win32 assembly because the example code I was following was written before the Environment assembly became available. So Thanks Peter for pointing out the Environment API.</p>\n'}, {'answer_id': 303019, 'author': 'Rob Mensching', 'author_id': 23852, 'author_profile': 'https://Stackoverflow.com/users/23852', 'pm_score': 1, 'selected': False, 'text': '<p>Why are you using a CustomAction for this?  The Windows Installer supports updating environment variables natively.</p>\n'}]
['https://Stackoverflow.com/questions/40840', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3114/']