Saturday, January 28, 2012

Reading & Writing registry key using C#


Reading registry key :


try
{
    RegistryKey registry = Registry.LocalMachine.CreateSubKey("SOFTWARE\\myKey");
    if (registry != null)
   {
      MessageBox.Show(registry.GetValue("myValue").ToString());
      registry.Close();
    }
}
catch (Exception ex)
{
   MessageBox.Show (ex.ToString());
}

Writing registry key :



try
{
RegistryKey registry = Registry.LocalMachine.CreateSubKey("SOFTWARE\\myKey");
if (registry != null)
{
registry.SetValue("myValue", "myReturnValue");
registry.Close();
}
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}