Reputation: 2982
I have a WOW scenario and want to change the value of Key at
HKLM\Software\Microsoft\ABCD\
I am using this code :
String key = @"SOFTWARE\Microsoft\ABCD\";
RegistryKey reg64key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey reg_64bit_Name = reg64key.OpenSubKey(key);
reg_64bit_Name.SetValue("Name","ahsan");
However this is not working. Can anyone kindly suggest what I need to do here ?
NB: 1. Not working means I am getting the following exception while running the app :
System.NullReferenceException: Object reference not set to an instance of an object.
Upvotes: 0
Views: 105
Reputation: 12683
Firstly, by "this is not working" can you please describe any error messages, exceptions compiler errors?
That being said your code has
reg64key.SetValue("Key","ahsan");
Where you will see you are using the "Key"
(as a string). Trying changing this to.
reg64key.SetValue(key,"ahsan");
So you are using your variable instead of the string "Key"
EDIT: After OP changed
After your edits I went back and tried this for myself. Please see the code below (this is tested)
RegistryKey reg64key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
String key = @"SOFTWARE\Microsoft\ABCD";
if (reg64key == null)
throw new Exception("reg64key");
var basekey = reg64key.OpenSubKey(key);
if (basekey == null)
basekey = reg64key.CreateSubKey(key);
basekey.SetValue("Name", "ahsan");
You will see from the code the first thing we do is grab the reg64key
for HKLM
. Next we check that the reg64key
is not null (shouldnt be null but you never know). Next we use the reg64key
to open the key "SOFTWARE\Microsoft\ABCD"
. If this is not found (baseKey == null
) then we create the key.
Finally you can set the key as you wish.
Hope this help.s
Upvotes: 1