Saving the TextBox Values in Registry

user1410658 picture user1410658 · May 22, 2012 · Viewed 18.6k times · Source

I need some guidance in reading/writing/saving the values in Registry.I am new to this concept of saving things in registry

I have a Winform where i have to read/write to a App.config file and change the username and password using a winform.In my winform i have 2 textboxes and when i enter values and hit submit it changes the values in app.config.I somehow did that and no issues.

Now I need to send what ever values I have entered in the Textboxes to registry and save them thr and I should also be able to read them.

How shoud I do that ?

Answer

Bali C picture Bali C · May 22, 2012

using Microsoft.Win32;

To write:

Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MyProgram", "Username", "User1");

To read:

string username = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MyProgram",
                                    "Username", "NULL").ToString();

In read where I have put NULL - thats the value to return if the value you are looking for isn't there.

So if you did:

if(username == "NULL")
{
    // it doesn't exist, handle situation here
}

Hope this helps.