Read And Write Registry Value using C#.

Hi Friends….

Here is the code for read the registry value using C#.

First I’m creating registry key value and string value in local machine as follows,

myKey --> key value
myValue --> string name
myReturnValue --> string value

Snapshots for registry values setting...





Code for Registry value Reading using C#...
using Microsoft.Win32;

try
{
RegistryKey registry = Registry.LocalMachine.CreateSubKey("SOFTWARE\\myKey");
if (registry != null)
{
MessageBox.Show(registry.GetValue("myValue"));
registry.Close();
}
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}

Code for Values writing in Registry using C#.

using Microsoft.Win32;

try
{
RegistryKey registry = Registry.LocalMachine.CreateSubKey("SOFTWARE\\myKey");
if (registry != null)
{
registry.SetValue("myValue", "myReturnValue");
registry.Close();
}
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}

Cheers....

... S.VinothkumaR.

2 comments:

RAHUL said...

With your example I am not able to read the registry value.

RAHUL said...

The issue was resolved and was related to Windows 7 64 bit machine.

Actually your code is working fine.

I need to change the target from x86 to Any CPU and it started working
http://stackoverflow.com/questions/6799584/accessing-registry-on-windows-7-c-sharp