Introduction
In this blog, we are going to demonstrate about registry reading, writing, counting and deleting the keys (like version) from registry for applications or installed software using C#. We can achieve this by using the below-mentioned source code. It will help you to understand the concept and implemenattion of the source code to read, write, and delete the version for installed software on local machines. Let's start.
Step 1 - Add common class for the application, let's say ModifyRegistry.cs class
Add Microsoft.Win32 namespace after adding ModifyRegistry.cs class
- using System;
- using Microsoft.Win32;
- using System.Reflection;
-
- namespace FrameworkHelper.Common
- {
-
-
-
- public class ModifyRegistry
- {
- private bool showError = false;
-
-
-
-
- public bool ShowError
- {
- get { return showError; }
- set { showError = value; }
- }
-
-
- private string subKey = "SOFTWARE\\Wow6432Node\\YourInstalledSoftwareApplicationName";
-
-
-
-
- public string SubKey
- {
- get { return subKey; }
- set { subKey = value; }
- }
-
- private RegistryKey baseRegistryKey = Registry.LocalMachine;
-
-
-
-
- public RegistryKey BaseRegistryKey
- {
- get { return baseRegistryKey; }
- set { baseRegistryKey = value; }
- }
-
-
-
-
-
-
- public string Read(string KeyName)
- {
-
- RegistryKey rk = baseRegistryKey;
-
- RegistryKey sk1 = rk.OpenSubKey(subKey);
-
- if (sk1 == null)
- {
- return null;
- }
- else
- {
- try
- {
-
-
- return (string)sk1.GetValue(KeyName.ToUpper());
- }
- catch (Exception e)
- {
-
- ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
- return null;
- }
- }
- }
-
-
-
-
-
- public bool Write(string KeyName, object Value)
- {
- try
- {
-
- RegistryKey rk = baseRegistryKey;
-
-
-
- RegistryKey sk1 = rk.CreateSubKey(subKey);
-
- sk1.SetValue(KeyName.ToUpper(), Value);
-
- return true;
- }
- catch (Exception e)
- {
-
- ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper());
- return false;
- }
- }
-
-
-
-
-
- public bool DeleteKey(string KeyName)
- {
- try
- {
-
- RegistryKey rk = baseRegistryKey;
- RegistryKey sk1 = rk.CreateSubKey(subKey);
-
- if (sk1 == null)
- return true;
- else
- sk1.DeleteValue(KeyName);
-
- return true;
- }
- catch (Exception e)
- {
-
- ShowErrorMessage(e, "Deleting SubKey " + subKey);
- return false;
- }
- }
-
-
-
-
-
- public bool DeleteSubKeyTree()
- {
- try
- {
-
- RegistryKey rk = baseRegistryKey;
- RegistryKey sk1 = rk.OpenSubKey(subKey);
-
- if (sk1 != null)
- rk.DeleteSubKeyTree(subKey);
-
- return true;
- }
- catch (Exception e)
- {
-
- ShowErrorMessage(e, "Deleting SubKey " + subKey);
- return false;
- }
- }
-
-
-
-
-
- public int SubKeyCount()
- {
- try
- {
-
- RegistryKey rk = baseRegistryKey;
- RegistryKey sk1 = rk.OpenSubKey(subKey);
-
- if (sk1 != null)
- return sk1.SubKeyCount;
- else
- return 0;
- }
- catch (Exception e)
- {
-
- ShowErrorMessage(e, "Retriving subkeys of " + subKey);
- return 0;
- }
- }
-
-
-
-
-
- public int ValueCount()
- {
- try
- {
-
- RegistryKey rk = baseRegistryKey;
- RegistryKey sk1 = rk.OpenSubKey(subKey);
-
- if (sk1 != null)
- return sk1.ValueCount;
- else
- return 0;
- }
- catch (Exception e)
- {
-
- ShowErrorMessage(e, "Retriving keys of " + subKey);
- return 0;
- }
- }
- private void ShowErrorMessage(Exception e, string Title)
- {
-
-
-
-
-
- }
-
- public static string AssemblyProductVersion
- {
- get
- {
- object[] attributes = Assembly.GetExecutingAssembly()
- .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
- return attributes.Length == 0 ?
- "" :
- ((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion;
- }
- }
- }
- }
Step 2
Now call the required method from class, use the below code to get or read the version from the registry of a local machine.
-
- which is mentioned in ModifyRegistry.cs class
-
-
- private string CurrentRegistryVersion()
- {
- ModifyRegistry mr = new ModifyRegistry();
- string currRegistry = mr.Read("InstallVersion");
- return currRegistry;
- }
Step 3
In the same way, you can call other methods like Write, Delete etc.
Thank you. I hope you like the blog and enjoyed the implementation of registry reading, writing, deleting and getting the count of application keys.
Enjoy coding.. :)