Read & Write to System Registry data, keys using C#

In this post i am going to explain that how to access the System Registry and how to read and write data to registry.

Step 1: First you need to import the below namespaces:
using Microsoft.Win32;

Step 2: Now create an object of Registry class:
RegistryKey regkey;

Step 3: Now access the registry and get the value of a key

regkey = Registry.CurrentUser.CreateSubKey(@”Software\Microsoft\FTP”);

if (regkey.GetValue(“Use PASV”) == null)
{
txtValue.Text = “No Value Specified”;
}

else
{
txtValue.Text = regkey.GetValue(“Use PASV”).ToString();
}

The above code gets the value of the “Use PASV” key from the
Registry\CurrentUser\Software\Microsoft\FTP path.

Step 4: Set the key value
regkey.SetValue(“Use PASV”, txtValue.Text);

7 Responses to “Read & Write to System Registry data, keys using C#”

  1. Umesh Says:

    I tried above code but i got regkey value null.

    Here is my code.

    RegistryKey regkey;
    regkey = Registry.CurrentUser.OpenSubKey(“HKEY_CURRENT_USER\\SOFTWARE\\MICROSOFT\\OFFICE\\12.0\\EXCEL\\SECURITY”, true);

    • dotnetcoderoom Says:

      Hi Umesh,

      You are using double slash in the registry path, use single slash instead.
      e.g.
      RegistryKey regkey;
      regkey = Registry.CurrentUser.OpenSubKey(”HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE\12.0\EXCEL\SECURITY”, true);

      I hope your code will work now.

  2. Gopal Says:

    I want to get remote computer registry key

  3. dotnetprome Says:

    This is reply to Umesh. As you told to use single slash instead double. But it gives error like “unrecognised escape sequence” in C#. Is it possible to type your scenario. Or is there any other way. Please suggest.

  4. Vallimala Says:

    Hi,
    Nice article.. :) Can you help me write a binary val to registry in c#. im new to the language and i searched through various articles in net but dint find any helpful tips:( If you could guide me thro if i’ll be happy! :)

    I need to change the second set of values for a particular key, when i set a value using the SeValue() of Microsoft.Win32 namespace, Im able to write oly the first set of values and that too appears in hexadecimal format in the reg editor

    Regards
    Mala

Leave a Reply