Write a Registry key

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
C# Code Snippets

C# Source Code Bank

See also …
edit

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();
   }
}




Personal tools