Thursday 12 May 2016

How to create registry key and value in C# ?

Once i was working with windows registry i was trying to check registry either it exists or not, and then is not had to
create registry with specific value. i was stuck with the creation of subkey and its drwtsn32 value.
To the frank with you i tried all possibility but it was to horrible for me to create subkey and open subkey again and again.
I was looking for something like loop which will create all subkey in single call without any problem.

Key which i had
HKEY_CURRENT_USER\SOFTWARE\Raj\subkey1\subkey2\subkey3
and then log = 1 as value

Initially i went through
http://stackoverflow.com/questions/4276138/how-to-check-if-a-registry-value-exists-using-c
http://www.codeproject.com/Questions/851650/Csharp-How-To-Check-If-Registry-Value-Exist
https://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey(v=vs.110).aspx
http://www.dotnetfunda.com/articles/show/856/how-to-create-a-key-in-the-registry-using-csharp
http://www.c-sharpcorner.com/UploadFile/f9f215/windows-registry/

But finally i made something easy for me, might be for you :)
C# code snippet to change registry in windows


using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nameof
{
    class Rededit_in_c_sharp
    {


        static RegistryKey createAllSubkey(string completeSubkey, RegistryKey rootreg)
        {
            RegistryKey _rtrn = null;
            StringBuilder sb = new StringBuilder();
            var stepreg = rootreg;
            var stestr = "";
            foreach (var item in completeSubkey.Split('\\'))
            {
                if (item != "")
                {
                    try
                    {

                        stestr += item + @"\";
                        var reg = stepreg.OpenSubKey(item, true);
                        if (reg == null)
                        {
                            stepreg.CreateSubKey(item);
                            stepreg = stepreg.OpenSubKey(item, true);
                        }
                        else
                        {
                            stepreg = stepreg.OpenSubKey(item, true);
                        }
                    }
                    catch (Exception exc){
                        Console.WriteLine(exc.Message);
                    }
                }
                _rtrn = stepreg;
            }

            return _rtrn;
        }





        static void Main(String[] args)
        {
            var rootreg = Registry.CurrentUser; //Registry.LocalMachine

            var str = @"SOFTWARE\Raj\subkey1\subkey2\subkey3";
            var myRegistry = createAllSubkey (str, rootreg);

            var keyval = myRegistry.GetValue("log", "0").ToString();
            if (keyval != "1")
            {
                myRegistry.SetValue("log", 1, RegistryValueKind.DWord);
            }

            Console.Read();
        }
    }
}