How To Read And Write Registry In C#

We will see how to read and write registry in c# today .
For people who don't know about registry if i tell it on my own words registry is like a registrar(the person who registers marriage) it registers all the software's that you install so it's like a registrar .


How to read registry ?
Actually reading a registry key is a simple and can be done with 3-4 lines of codes.I am doing this in Graphical User Interface program yes a Windows Forms Project . Add the namespace using Microsoft.Win32; now make sure what you want to read and where you want to read(Local Machine,Current User,etc) , I have no idea what to read so i will read a Google Chrome Extension registered here Google > Chrome > Extensions > dmibjfmphcpfoacbchialfobiohmhged 
How To Read And Write Registry In C#
So as you can see the path in registry now i write 3 lines of codes to read the registry key path(the path of the extension) and show it in a Message Box .
    RegistryKey path = Registry.LocalMachine.OpenSubKey("Software\\Google\\Chrome\\Extensions\\dmibjfmphcpfoacbchialfobiohmhged");//path of the registry
            string read = (string)path.GetValue("path");//creating a string to get the value of 'path'
            MessageBox.Show(read,"Registry Key Value", MessageBoxButtons.OK,MessageBoxIcon.Information);//the messagebox pops with the key 'path' value
I am sorry i couldn't explain everything in the code , as you can see in the first line we define where our key is  "Local Machine" you can put anything places as in your registry for that by the way remember when you read or write registry you should use double // for the destination .

Now when i debug here the Message Box pops up with the 'path' value.
How To Read And Write Registry In C#
Done with reading .

How to write registry?
As it's very easy to read registry , it's easy to write registry by the way i can't write my key into the path i read because it Google's so i chose Current User path to write the registry key. As we did before (reading) add the namespace using Microsoft.Win32; now i will create a registry path and i will add a value called , my whole path is HKEY_CURRENT_USER\Software\Shim\Ultimate_programming_tutorials . ok now i will write the code to write the registry key.
RegistryKey set = Registry.CurrentUser.CreateSubKey("Software\\Shim\\Ultimate_programming_tutorials");//setting the path
            set.SetValue("website", "http://ultimateprogrammingtutorials.blogspot.com");//setting the value
            MessageBox.Show("Value has been added " + set,"Value Added",MessageBoxButtons.OK,MessageBoxIcon.Information);//creating a messagebox to show it's added(value)
            set.Close();//closing the current process
website is the name of the value we are setting and http://ultimateprogrammingtutorials.blogspot.com is the value of work :).

Now when i debug a Message Box pops up saying value has been added and the path that the value was added.
How To Read And Write Registry In C#
Now when i look the path , the value is there .
How To Read And Write Registry In C#
Done with writing :) . I hope this is very useful :)

Post a Comment

Note: Only a member of this blog may post a comment.