Read a Value from the Windows Registry in C# 5.0

In the previous post we saw how to create a registry i.e. write a value into the Windows registry. Now we shall retrieve back the exact value that we had written before. To do this we need to retrieve the value in a String variable.

We shall take the value by its exact location & the name of the property. Note that the property resides in a folder and the value we read is the Data of that property. Have a look at the present snapshot of the system:

  • Root Folder is HKEY_CURRENT_USER
  • Parent Folder is CSharp_Website
  • Property/Attribute/Key Name is Really
  • Property/Attribute/Key Value is Yes!

The GetValue() function does this work impressively for us. Considering the values we have above, we create a String that provides the correct path for the Key Value. The code is shown below:



private void btnReadReg_Click(object sender, EventArgs e)
        {
            string x = (string)Registry.GetValue("HKEY_CURRENT_USER\\CSharp_Website","Really","DefaultValue");
            MessageBox.Show(x,"Read Registry");
           
        }