65.9K
CodeProject is changing. Read more.
Home

Writing Registry using C#

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.43/5 (6 votes)

Dec 5, 2011

CPOL
viewsIcon

26150

Writing Registry using C#

This C# code snippet writes a key to the Windows Registry.

WARNING: Do NOT play with the Registry. If you do not know what you are doing, you can cause any number of costly problems. Use this code at your own risk.

using Microsoft.Win32;
...

RegistryKey masterKey = Registry.LocalMachine.CreateSubKey
   ("SOFTWARE\\Test\\Preferences");
if (masterKey == null)
{
   Console.WriteLine ("Null Masterkey!");
}
else
{
   try
   {
      masterKey.SetValue ("MyKey", "MyValue");
   }
   catch (Exception ex)
   {
      Console.WriteLine (ex.Message);
   }
   finally
   { 
      masterKey.Close();
   }
}