Reading and writing to the registry through c#.net

Basic code to read from teh registry and write to the registry using c#.net.

Declare two strings:

string
strWindowsState,
strPath;

Here is the code to Read from the Registry:


RegistryKey
regKeyAppRoot = Registry.CurrentUser.CreateSubKey(strPath);
 

             strWindowsState = (string)regKeyAppRoot.GetValue("WindowState"); 

            if (strWindowsState != null && strWindowsState.CompareTo("Maximized") == 0)

                WindowState = System.Windows.Forms.FormWindowState.Maximized; 

            else if (strWindowsState != null && strWindowsState.CompareTo("Minimized") == 0)

                WindowState = System.Windows.Forms.FormWindowState.Minimized; 

            else

                WindowState = FormWindowState.Normal;

            label1.Text = strWindowsState;

            label3.Text = "";

            return;

Writing to the Registry:

strWindowsState = "";

            RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey(strPath); 

            if (WindowState == FormWindowState.Maximized)

                strWindowsState = "Maximized"; 

            else if(WindowState == FormWindowState.Maximized)

                strWindowsState = "Minimized"; 

            else

                 strWindowsState = "Normal";

 

             regKeyAppRoot.SetValue("WindowState", strWindowsState);

             label3.Text = strWindowsState;

             label1.Text = "";

             return;

To test:
After running the application change the window state of the form to minimized then click on write button.....it will write into Registry.... Then click on read button..... you can see that the state value is changing

By alice johnson   Popularity  (2985 Views)