eHow launches Android app: Get the best of eHow on the go.

How To

How to Write to the Windows Registry with Registry.SetValue in Microsoft Visual C # (C Sharp)

Member
By Derek Hardenbergh
User-Submitted Article
(1 Ratings)
Write to the Windows Registry with Registry.SetValue in Microsoft Visual C # (C Sharp)
Write to the Windows Registry with Registry.SetValue in Microsoft Visual C # (C Sharp)

Write to the Windows registry using the Registry.SetValue method in the Microsoft.Win32 namespace in C#.

Difficulty: Moderate
Instructions

Things You'll Need:

  • Microsoft Visual C# 2008 Express (free)
  1. Step 1
     

    WARNING: You can seriously mess up your computer by altering the registry. Make a backup of the registry prior to altering it in any way. You have been warned!

    First we need to examine the location in the registry where we will be writing our data. Click on Start and then Run. Type in regedit and click OK.

  2. Step 2
     

    Navigate to HKEY_CURRENT_USER\Environment and examine the contents. This is where will will write our data. Keep this program open because we will return here after we run our program.

  3. Step 3
     

    Note: This article assumes you have installed Microsoft Visual C# 2008 Express Edition. You may download it for free from here: http://www.microsoft.com/express/download/

    Open Microsoft Visual C#. Click on "Project..." to the right of Create in the Recent Projects area of the Start Page.

    The New Project window will open. Click on "Windows Forms Application", enter a Name, and click OK.

    By default, the only form in the project will be called "Form1" and you will be in Design mode for that form.

  4. Step 4
     

    Hover over the Toolbox on the left side of the screen and the Toolbox will automatically expand. Click and drag a Button control, under the Common Controls category, onto the form.

  5. Step 5
     

    Double click the button and you will now be in the code window for Form1. The method for the button click will already be created.

  6. Step 6
     

    Add "using Microsoft.Win32;" just beneath "using System.Windows.Forms;". This namespace contains the Registry object that has the SetValue method that we will be using.

  7. Step 7
     

    In the button1_Click method, place the following code:

    try{
    string MyKeyName = "HKEY_CURRENT_USER\\Environment";
    string MyValueName = "BestLang";
    string MyString = "C#";

    Registry.SetValue(MyKeyName,MyValueName,MyString);

    MessageBox.Show("Done.");
    }
    catch(Exception MyError){
    MessageBox.Show("Error reading file: "+ MyError.Message);
    }

    The try/catch statements will capture any error that occurs and will pop-up a MessageBox with the error displayed.

    The Registry.SetValue method takes two strings and an object for parameters. For our example, the third object will be another string. But you could write numeric data as well.

    The method returns nothing so we will just display a MessageBox that it is done.

    Note that the string MyKeyName has a double slash separating the registry keys. This is due to a single slash being a special character in a string.

  8. Step 8
     

    Go up to the toolbar and run the program by clicking on the Start Debugging (f5) play button.

    NOTE: If you got any kind of error after clicking the play button, you probably made a syntax error when typing the code. Reread the code until you find and correct the error and try again.

  9. Step 9
     

    The form will take a moment and then pop up. Click on the button and you will get a pop-up stating that it is done.

  10. Step 10
     

    Go back to the Registy Editor and hit F5 to refresh the view. You should now see the new data. Awesome!

Tips & Warnings
  • Play around with the Help. Tons of good stuff in there.
  • You might get addicted to C#. Watch out Java and C++ folks!!!
  • You can seriously mess up your computer by altering the registry. Make a backup of the registry prior to altering it in any way. You have been warned!
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics