Writing Registry from C#

This example adds the value pair, "Name" and "Isabella", to the current user's registry, under the key "Names".

Code Snippet
  1. Microsoft.Win32.RegistryKey key;
  2. key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Names");
  3. key.SetValue("Name", "Isabella");
  4. key.Close();

Steps to follow :
  • Copy the code and paste it into the Main method of a console application.
  • Replace the Names parameter with the name of a key that exists directly under the   HKEY_CURRENT_USER node of the registry.
  • Replace the Name parameter with the name of a value that exists directly under the Names node.

It is more secure to write data to the user folder — Microsoft.Win32.Registry.CurrentUser — rather than to the local computer — Microsoft.Win32.Registry.LocalMachine.

Post a Comment