Saving the TextBox Values in Registry
Asked Answered
F

2

13

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 ?

Felisafelise answered 22/5, 2012 at 16:6 Comment(2)
Plenty of info about this on Google, heres something to start withSabinesabino
Can't you make some simple google search ?Gipsy
M
15

Here is a quick code:

private void button1_Click(object sender, EventArgs e)
{
    Microsoft.Win32.RegistryKey exampleRegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("ExampleTest");
    exampleRegistryKey.SetValue("Name", textBox1.Text);
    exampleRegistryKey.Close();
}

Now if you run regedit and must see under HKEY_CURRENT_USER\ExampleTest

Miscall answered 22/5, 2012 at 16:45 Comment(0)
K
34

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.

Karolinekaroly answered 22/5, 2012 at 16:11 Comment(4)
Yes Buddy.Thank you.... Do you have any Example to display here about this registry read/write/save ??Felisafelise
@Felisafelise No probs. Updated question with actual data. If you had a key named MyProgram and you set the value Username with the first code and you get the value using the second. Once you make the change you don't need to save it.Karolinekaroly
@Felisafelise No problem, if this answers your question you should accept it, just click the tick :DKarolinekaroly
Just to note: The route may translate to: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MyProgram.Bothwell
M
15

Here is a quick code:

private void button1_Click(object sender, EventArgs e)
{
    Microsoft.Win32.RegistryKey exampleRegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("ExampleTest");
    exampleRegistryKey.SetValue("Name", textBox1.Text);
    exampleRegistryKey.Close();
}

Now if you run regedit and must see under HKEY_CURRENT_USER\ExampleTest

Miscall answered 22/5, 2012 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.