Example in c#
using System;
using System.Security.Permissions;
using Microsoft.Win32;
class RegKey
{
static void Main()
{
// Create a subkey named Testkey under HKEY_CURRENT_USER.
RegistryKey testkey =
Registry.CurrentUser.CreateSubKey("Testkey");
// Create two subkeys under HKEY_CURRENT_USER\Testkey. The
// keys are disposed when execution exits the using statement.
using(RegistryKey
testName = testkey.CreateSubKey("TestName"),
testSettings = testkey.CreateSubKey("TestSettings"))
{
// Create data for the TestSettings subkey.
testSettings.SetValue("Language", "Hindi");
testSettings.SetValue("Level", "Class");
testSettings.SetValue("ID", 1);
}
// Print the information from the Testkey subkey.
Console.WriteLine("There are {0} subkeys under {1}.",
testkey.SubKeyCount.ToString(), testkey.Name);
foreach(string subKeyName in testkey.GetSubKeyNames())
{
using(RegistryKey
tempKey = testkey.OpenSubKey(subKeyName))
{
Console.WriteLine("\nThere are {0} values for {1}.",
tempKey.ValueCount.ToString(), tempKey.Name);
foreach(string valueName in tempKey.GetValueNames())
{
Console.WriteLine("{0,-8}: {1}", valueName,
tempKey.GetValue(valueName).ToString());
}
}
}
using(RegistryKey
testSettings = testkey.OpenSubKey("TestSettings", true))
{
// Delete the ID value.
testSettings.DeleteValue("id");
// Verify the deletion.
Console.WriteLine((string)testSettings.GetValue(
"id", "ID not found."));
}
// Delete or close the new subkey.
Console.Write("\nDelete newly created registry key? (Y/N) ");
if(Char.ToUpper(Convert.ToChar(Console.Read())) == 'Y')
{
Registry.CurrentUser.DeleteSubKeyTree("Testkey");
Console.WriteLine("\nRegistry key {0} deleted.",
testkey.Name);
}
else
{
Console.WriteLine("\nRegistry key {0} closed.",
testkey.ToString());
key.Close();
}
}
}