Saving TextBox Values โ€‹โ€‹in the Registry

I need some guidance on reading / writing / storing values โ€‹โ€‹in the Registry. I am new to this concept of keeping things in the registry.

I have Winform where I have to read / write to the App.config file and change the username and password using winform. In my winform, I have 2 text fields, and when I enter the values โ€‹โ€‹and click submit, they change the values โ€‹โ€‹in app.config.I somehow did it and no problem.

Now I need to send any values โ€‹โ€‹that I entered into the text fields in the registry and save them, and I also have to read them.

How am i doing this?

+8
c # winforms registry read-write
source share
2 answers

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 should see under HKEY_CURRENT_USER\ExampleTest

+12
source share

using Microsoft.Win32;

To write:

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

Think:

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

In the read place where I put NULL , this is the value returned if the value you are looking for does not exist.

So, if you did this:

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

Hope this helps.

+30
source share

All Articles