Registry Keys not being created when using SetValue

c#, registry

Solution

RK must be opened with write access :

RK = RK.OpenSubKey("P",true);

with no the second argument, it is in read only.

Problem

A program I am writing uses a number of registry keys in order to store some important information. In order to ensure that all of the registry keys exist in the first method of my program I test if the registry keys exist and create them if they do not with a default value. Here is my code: ``` RegistryKey RK = Registry.LocalMachine.OpenSubKey("Software"); if (RK.OpenSubKey("NR") == null) RK.CreateSubKey("NR"); RK = RK.OpenSubKey("NR"); if (RK.OpenSubKey("P") == null) RK.CreateSubKey("P"); RK = RK.OpenSubKey("P"); if (RK.GetValue("BP") == null) RK.SetValue("BP", "B{1}-{0}"); ``` The problem is that even when I run this on a computer without these keys, they are not being created. The program is set to always run as administrator and it is running on a Windows XP computer. So using the code above the SubKey NR\P exists but BP does not, after running this code BP still does not exist. Can anyone see what is wrong?

Original source