Loading...

Using Registry Function From Registry Editor in C#

Using Registry Function From Registry Editor in C# Using namespace Microsoft.Win32

What is Registry Function in C#:

Registry Function Provides RegistryKey objects that represent the root keys in the Windows registry, and static methods to access key/value pairs. ~definition by msdn

Registry Function is  a very important and sensitive feature for which you will need the administrative rights in Your Operating System like Windows and also in Microsoft Visual Studio. For this do right click on Visual Studio exe file and click on run as administrator.

Starting Registry Functions:

First of all, you have to add a namespace Microsoft.Win32 at starting point like in the image given below:

Suppose we are here to store the height and width of a Windows Form Application at the time of closing and open with the same width and height when user open it again

Reading Values of width and height from Registry:

Write the below function in the event of Form Closing of Windows Form Application to Store Width and height:
 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            int width = this.Width;//initializing and setting value of width of Form


            int height = this.Height;//initializing and setting value of width of Form

            RegistryKey software = Registry.LocalMachine.OpenSubKey("SOFTWARE", true); // accessing software Registry, if it exists 

            RegistryKey RegEx = software.CreateSubKey("RegDotNet");//accessing "RegDotNet" subdirectory in software registry

            RegistryKey sizekey = RegEx.CreateSubKey("Size");//accessing "size" subdirectory further

            sizekey.SetValue("Width", width.ToString());// storing values of width in size 
            sizekey.SetValue("height", height.ToString());// storing values of height in size 

        }

Storing Values of width and height in Registry:

Call the below method in constructor of Form to read the width and height which was stored at the time of closing the application last time.


public void store_registry()

        {

            int width;

            int height;            

            RegistryKey software = Registry.LocalMachine.OpenSubKey("SOFTWARE");// Accessing a Registery whose name is Software

            RegistryKey regEx = software.OpenSubKey("RegDotNet");// Accessing  a subdirectory RegDotNet in Directory "Software

            if (regEx != null) // Codition to check nullity
            {
                RegistryKey sizekey = regEx.OpenSubKey("size");// Opening sizekey Subdirectory of RegDotNet

                if (sizekey != null)// Condition to check nullti of size subdirectory

                {
                    width = Convert.ToInt32(sizekey.GetValue("width").ToString());// getting value  on width stored in "size" subdirectory

                    height = Convert.ToInt32(sizekey.GetValue("height").ToString());// getting value  on height stored in "size" subdirectory

                    this.Width=width; // manipulating value of width in "size" subdirectory

                    this.Height=height;// manipulating value of height in "size" subdirectory


                }
            }

        }


Output:

Now the Form will be opening in the same size as the user close it last time... Thank You 


How to see Stored Keys in Registry Editor using run:

Thanks for Waiting... Go to this link to see very easy step by step guide.

See Store Key From Registry Editor

This article is taken from our this Blog

Hit the Facebook like button below if you like this article... this helps a lot and keep us motivated :)



Google+

 
TOP