Wednesday, August 6, 2008

How to Read/Write registry in C#?

Reading and writing to a registry is a routine task that an application developer has to perform using the C# code. Here is a handy way to read/write Windows registry using the C# code.

Required namespace:



using Microsoft.Win32;

Simple Way to Read/Write Registry:



public string GetRegistryValue(string key)
{
return Convert.ToString(Registry.GetValue
(@"HKEY_CURRENT_USER\Software\Yahoo\Common", "Sandy", "OLD"));
}

public void SetRegistryValue()
{
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Yahoo\Common", "Sandy", NEW");
}

The above given is a simple method for accessing the registry. More complex class can be written to leverage advance registry functionality.

4 comments:

Kuldeep Chowhan said...

Good Post

Anonymous said...

Thanks

Unknown said...

Thanks!

Registry.SetValue(@"HKEY_CURRENT_USER\Software\Yahoo\Common", "Sandy", NEW");

is missing a " before NEW

Anonymous said...

Registry.SetValue(@"HKEY_CURRENT_USER\Software\Yahoo\Common", "Sandy", NEW");

missing a " before NEW

Post a Comment