Thursday, 20 March 2014

c# code to add registry key

Some time we need to make our application as startup application or we need to configure our application with registry setting on this case we need to play with registry and have to Read, Write and delete the registry entry,
Just demonstrating in simple way, how to Read, write and delete registry entry in c# code;
Before doing the code you need to import the three references:
using Microsoft.Win32;
using System.Text;
using System.Windows.Forms;
Adding the Key into registry in C#;
///
        ///   ("UtilityApp", "C:\Program Files\WordWeb\Utility.exe -startup")
        ///
        public bool Write(string KeyName, object Value)
        {
            try
            {
                // Setting
                RegistryKey rk = baseRegistryKey;
                // 'OpenSubKey open a subKey as read-only
                RegistryKey sk1 = rk.CreateSubKey(subKey);
               
                sk1.SetValue(KeyName.ToUpper(), Value); // Setting the value
                return true;
            }
            catch (Exception e)
            {
                return false; //If any error finds
            }
        }
Deleting the Key from registry in C#;
///
        /// Deleting the key from registry
        ///

        ///
        ///
        public bool DeleteKey(string KeyName)
        {
            try
            {
                // Setting
                RegistryKey rk = baseRegistryKey;
                RegistryKey sk1 = rk.CreateSubKey(subKey);
              
                if (sk1 == null)
                    return true;
                else
                    sk1.DeleteValue(KeyName);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
Reading registry key in C#;
public string Read(string KeyName)
        {
            RegistryKey rk = baseRegistryKey;// Opening the registry key
            RegistryKey sk1 = rk.OpenSubKey(subKey);   // Open a subKey as read-only
            // If the RegistrySubKey doesn't exist
            if (sk1 == null)
            {
                return null;
            }
            else
            {
                try
                {
                    return (string)sk1.GetValue(KeyName.ToUpper()); // If the RegistryKey exists I get its value
                }
                catch (Exception e)
                {
                    return null;
                }
            }
        }
@Full code for read, write and delete the registry key in c#
namespace SystemTrayApp.Classes
{
    class ReadWritetoRegistry
    {
        private string subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\" ; //+ Application.ProductName.ToUpper();
      
        public string SubKey
        {
            get { return subKey; }
            set { subKey = value; }
        }
        private RegistryKey baseRegistryKey = Registry.CurrentUser;
        public string Read(string KeyName)
        {
            RegistryKey rk = baseRegistryKey;// Opening the registry key
            RegistryKey sk1 = rk.OpenSubKey(subKey);   // Open a subKey as read-only
            // If the RegistrySubKey doesn't exist
            if (sk1 == null)
            {
                return null;
            }
            else
            {
                try
                {
                    return (string)sk1.GetValue(KeyName.ToUpper()); // If the RegistryKey exists I get its value
                }
                catch (Exception e)
                {
                    return null;
                }
            }
        }
        ///
        ///
        ///

        ///
        ///   ("UtilityApp", "C:\Program Files\WordWeb\Utility.exe -startup")
        ///
        public bool Write(string KeyName, object Value)
        {
            try
            {
                // Setting
                RegistryKey rk = baseRegistryKey;
                // 'OpenSubKey open a subKey as read-only
                RegistryKey sk1 = rk.CreateSubKey(subKey);
               
                sk1.SetValue(KeyName.ToUpper(), Value); // Setting the value
                return true;
            }
            catch (Exception e)
            {
                return false; //If any error finds
            }
        }
        ///
        /// Deleting the key from registry
        ///

        ///
        ///
        public bool DeleteKey(string KeyName)
        {
            try
            {
                // Setting
                RegistryKey rk = baseRegistryKey;
                RegistryKey sk1 = rk.CreateSubKey(subKey);
              
                if (sk1 == null)
                    return true;
                else
                    sk1.DeleteValue(KeyName);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public int SubKeyCount()
        {
            try
            {
                // Setting
                RegistryKey rk = baseRegistryKey;
                RegistryKey sk1 = rk.OpenSubKey(subKey);
                // If the RegistryKey exists...
                if (sk1 != null)
                    return sk1.SubKeyCount;
                else
                    return 0;
            }
            catch (Exception e)
            {
                return 0;
            }
        }
    }
}
@How to call it to write the registry;
It detect and run time bind the exe file
ReadWritetoRegistry objReg = new ReadWritetoRegistry();
            objReg.DeleteKey("UtilityApp");
            objReg.Write("UtilityApp", path.Replace("file:\\","") + "\\desktoputility.exe -startup");
@How output will display for registry;

No comments:

Post a Comment

Others

You will like to read it