Reputation: 35
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?
Upvotes: 2
Views: 6189
Reputation: 4104
RK must be opened with write access :
RK = RK.OpenSubKey("P",true);
with no the second argument, it is in read only.
Upvotes: 6
Reputation: 9639
When calling SetValue, you need to open the key for writing by using the correct OpenSubKey method.
Upvotes: 0