Link to home
Start Free TrialLog in
Avatar of Nasser Hamdan
Nasser HamdanFlag for United Arab Emirates

asked on

Change Registry Value using C#

Hi,

I have desktop c# application, and when this application main frm loads i want to update some registry value to null ...
the regsitry value resided on:

HKEY_CURRENT_USER\AppEvents\Schemes\Apps\.Default\.Default\.Current

This key have default vallue as (%SystemRoot%\media\Windows XP Ding.wav)

And i  just want to replace it with null ! i tried this and didnt work !



RegistryKey rkHKLM = Registry.CurrentUser; ;            
RegistryKey rkRun;
 
rkRun =
rkHKLM.OpenSubKey(@"HKEY_CURRENT_USER\AppEvents\Schemes\Apps\.Default\.Default\.Current", true);
 
rkRun.SetValue("Default", "");                      
Registry.CurrentUser.Flush();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fernando Soto
Hi hammdan_1980;

As per the documentation:
Since many values can be stored in each key in the registry, the name parameter specifies the particular value you want to manipulate. To set the default value for a particular registrykey, name can be set to either a null reference (Nothing in Visual Basic), or the empty string ("").

http://msdn2.microsoft.com/en-us/library/microsoft.win32.registrykey.setvalue(VS.71).aspx

So change this:
    rkRun.SetValue("Default", "");
To this:
    rkRun.SetValue("", "");
or this:
    rkRun.SetValue(null, "");

Fernando
Avatar of Nasser Hamdan

ASKER

Hi Fernando,
I tried the solution as well read the article,, and tried what is suggesting, but i faced same problem which is exception saying: Object refrence not set to an instance of object.

here is the code again after modify it:




RegistryKey rkHKLM = Registry.CurrentUser;
 
RegistryKey rkRun;
rkRun =
rkHKLM.OpenSubKey(@"HKEY_CURRENT_USER\AppEvents\Schemes\Apps\.Default\.Default\.Current", true);
 
rkRun.SetValue(null, "");              
Registry.CurrentUser.Flush();

Open in new window

Thanks alot ... i cant believe that it was so easy and i spent 4 hours try to figure it out :(