Read/Write Registry Keys

by Shabbir on May 5, 2008

in C#

Writing and reading to/from a registry key in .Net is really easy. All that is implemented in Microsoft.Win32 namespace, so at the start put this in the usings

using Microsoft.Win32;

Than in some function write this

RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true)
.CreateSubKey("CodeItWell.com");

So, we are creating an object from the RegistryKey class, the object will open the sub key Software in HKEY_CURRENT_USER, and it will create a key there with the name CodeItWell.com.

Now lets create some values in that key

key.SetValue("URL", "http://www.codeitwell.com");
key.SetValue("Number", 1234, RegistryValueKind.DWord);

Here we create a string value URL with data http://www.codeitwell.com, and a DWORD value with its value 1234.

You can set many value kinds... see the picture below

Registry Value Kinds

Now open the registry editor Start -> Run -> Write "regedit" -> OK. Go to this path HKEY_CURRENT_USER\Software\CodeItWell.com, you will see the values on the right panel

Registry Editor

To read the registry values we will use the same object, see this

MessageBox.Show(key.GetValue("URL").ToString(), "URL");

You will get a message box showing the data in URL value. Try to change that data manually in the registry editor, and then call the event where the above line of code is entered and you will see the data will be changed.

Sharing is Caring...

Other Related Posts:

  1. Usage Of RegistryKey Class
  2. Read From File In C++
  3. Write To File in C#
  4. Read From File Using StreamReader Class
  5. Write To File In C++

{ 4 comments… read them below or add one }

registry cleaner January 14, 2010 at 1:52 pm

You saved my alot of time thank you so much for sharing your experience.

Reply

Best reg cleaner July 12, 2010 at 3:32 am

I have been using reg cleaners for a long time, and it's good as well.

Reply

Best registry cleaner August 18, 2010 at 10:36 am

This is a post about this subject I really wanted to read.

Reply

Registry scanner November 6, 2010 at 3:01 am

:razz: Hey mate, thanks for writing but this page doesn’t format correctly in Chrome it is showing only half the page.

Reply

Leave a Comment

Spam protection by WP Captcha-Free

Previous post:

Next post:

    About the Author

  • author photo

    My Name is Shabbir Bhimani and I am developer by profession in the field of applications, web and database. Currently doing full time online marketing and like to share my experiences on how you can make money online @ CodeItWell.com. Read more ...


    See how you can get in touch with me.