Read, Write Or Delete Software Version From Registry Using C#

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
  1. using System;  
  2. using Microsoft.Win32;  
  3. using System.Reflection;  
  4.   
  5. namespace FrameworkHelper.Common  
  6. {  
  7.     /// <summary>  
  8.     /// An useful class to read/write/delete/count registry keys  
  9.     /// </summary>  
  10.     public class ModifyRegistry  
  11.     {  
  12.         private bool showError = false;  
  13.         /// <summary>  
  14.         /// A property to show or hide error messages   
  15.         /// (default = false)  
  16.         /// </summary>  
  17.         public bool ShowError  
  18.         {  
  19.             get { return showError; }  
  20.             set { showError = value; }  
  21.         }  
  22.   
  23.         /// Please set your installed software name in below subkey variable 
  24.         private string subKey = "SOFTWARE\\Wow6432Node\\YourInstalledSoftwareApplicationName";  
  25.         /// <summary>  
  26.         /// A property to set the SubKey value  
  27.         /// (default = "SOFTWARE\\" + Application.ProductName.ToUpper())  
  28.         /// </summary>  
  29.         public string SubKey  
  30.         {  
  31.             get { return subKey; }  
  32.             set { subKey = value; }  
  33.         }  
  34.   
  35.         private RegistryKey baseRegistryKey = Registry.LocalMachine;  
  36.         /// <summary>  
  37.         /// A property to set the BaseRegistryKey value.  
  38.         /// (default = Registry.LocalMachine)  
  39.         /// </summary>  
  40.         public RegistryKey BaseRegistryKey  
  41.         {  
  42.             get { return baseRegistryKey; }  
  43.             set { baseRegistryKey = value; }  
  44.         }  
  45.   
  46.         /// <summary>  
  47.         /// To read a registry key.  
  48.         /// input: KeyName (string)  
  49.         /// output: value (string)   
  50.         /// </summary>  
  51.         public string Read(string KeyName)  
  52.         {  
  53.             // Opening the registry key  
  54.             RegistryKey rk = baseRegistryKey;  
  55.             // Open a subKey as read-only  
  56.             RegistryKey sk1 = rk.OpenSubKey(subKey);  
  57.             // If the RegistrySubKey doesn't exist -> (null)  
  58.             if (sk1 == null)  
  59.             {  
  60.                 return null;  
  61.             }  
  62.             else  
  63.             {  
  64.                 try  
  65.                 {  
  66.                     // If the RegistryKey exists I get its value  
  67.                     // or null is returned.  
  68.                     return (string)sk1.GetValue(KeyName.ToUpper());  
  69.                 }  
  70.                 catch (Exception e)  
  71.                 {  
  72.                     // AAAAAAAAAAARGH, an error!  
  73.                     ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());  
  74.                     return null;  
  75.                 }  
  76.             }  
  77.         }  
  78.         /// <summary>  
  79.         /// To write into a registry key.  
  80.         /// input: KeyName (string) , Value (object)  
  81.         /// output: true or false   
  82.         /// </summary>  
  83.         public bool Write(string KeyName, object Value)  
  84.         {  
  85.             try  
  86.             {  
  87.                 // Setting  
  88.                 RegistryKey rk = baseRegistryKey;  
  89.                 // I have to use CreateSubKey   
  90.                 // (create or open it if already exits),   
  91.                 // 'cause OpenSubKey open a subKey as read-only  
  92.                 RegistryKey sk1 = rk.CreateSubKey(subKey);  
  93.                 // Save the value  
  94.                 sk1.SetValue(KeyName.ToUpper(), Value);  
  95.   
  96.                 return true;  
  97.             }  
  98.             catch (Exception e)  
  99.             {  
  100.                 //an error!  
  101.                 ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper());  
  102.                 return false;  
  103.             }  
  104.         }  
  105.         /// <summary>  
  106.         /// To delete a registry key.  
  107.         /// input: KeyName (string)  
  108.         /// output: true or false   
  109.         /// </summary>  
  110.         public bool DeleteKey(string KeyName)  
  111.         {  
  112.             try  
  113.             {  
  114.                 // Setting  
  115.                 RegistryKey rk = baseRegistryKey;  
  116.                 RegistryKey sk1 = rk.CreateSubKey(subKey);  
  117.                 // If the RegistrySubKey doesn't exists -> (true)  
  118.                 if (sk1 == null)  
  119.                     return true;  
  120.                 else  
  121.                     sk1.DeleteValue(KeyName);  
  122.   
  123.                 return true;  
  124.             }  
  125.             catch (Exception e)  
  126.             {  
  127.                 // AAAAAAAAAAARGH, an error!  
  128.                 ShowErrorMessage(e, "Deleting SubKey " + subKey);  
  129.                 return false;  
  130.             }  
  131.         }  
  132.         /// <summary>  
  133.         /// To delete a sub key and any child.  
  134.         /// input: void  
  135.         /// output: true or false   
  136.         /// </summary>  
  137.         public bool DeleteSubKeyTree()  
  138.         {  
  139.             try  
  140.             {  
  141.                 // Setting  
  142.                 RegistryKey rk = baseRegistryKey;  
  143.                 RegistryKey sk1 = rk.OpenSubKey(subKey);  
  144.                 // If the RegistryKey exists, I delete it  
  145.                 if (sk1 != null)  
  146.                     rk.DeleteSubKeyTree(subKey);  
  147.   
  148.                 return true;  
  149.             }  
  150.             catch (Exception e)  
  151.             {  
  152.                 // an error!  
  153.                 ShowErrorMessage(e, "Deleting SubKey " + subKey);  
  154.                 return false;  
  155.             }  
  156.         }  
  157.         /// <summary>  
  158.         /// Retrive the count of subkeys at the current key.  
  159.         /// input: void  
  160.         /// output: number of subkeys  
  161.         /// </summary>  
  162.         public int SubKeyCount()  
  163.         {  
  164.             try  
  165.             {  
  166.                 // Setting  
  167.                 RegistryKey rk = baseRegistryKey;  
  168.                 RegistryKey sk1 = rk.OpenSubKey(subKey);  
  169.                 // If the RegistryKey exists...  
  170.                 if (sk1 != null)  
  171.                     return sk1.SubKeyCount;  
  172.                 else  
  173.                     return 0;  
  174.             }  
  175.             catch (Exception e)  
  176.             {  
  177.                 //an error!  
  178.                 ShowErrorMessage(e, "Retriving subkeys of " + subKey);  
  179.                 return 0;  
  180.             }  
  181.         }  
  182.         /// <summary>  
  183.         /// Retrive the count of values in the key.  
  184.         /// input: void  
  185.         /// output: number of keys  
  186.         /// </summary>  
  187.         public int ValueCount()  
  188.         {  
  189.             try  
  190.             {  
  191.                 // Setting  
  192.                 RegistryKey rk = baseRegistryKey;  
  193.                 RegistryKey sk1 = rk.OpenSubKey(subKey);  
  194.                 // If the RegistryKey exists...  
  195.                 if (sk1 != null)  
  196.                     return sk1.ValueCount;  
  197.                 else  
  198.                     return 0;  
  199.             }  
  200.             catch (Exception e)  
  201.             {  
  202.                 //an error!  
  203.                 ShowErrorMessage(e, "Retriving keys of " + subKey);  
  204.                 return 0;  
  205.             }  
  206.         }  
  207.         private void ShowErrorMessage(Exception e, string Title)  
  208.         {  
  209.             //if (showError == true)  
  210.             //    MessageBox.Show(e.Message,  
  211.             //                    Title  
  212.             //                    , MessageBoxButtons.OK  
  213.             //                    , MessageBoxIcon.Error);  
  214.         }  
  215.   
  216.         public static string AssemblyProductVersion  
  217.         {  
  218.             get  
  219.             {  
  220.                 object[] attributes = Assembly.GetExecutingAssembly()  
  221.                     .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);  
  222.                 return attributes.Length == 0 ?  
  223.                     "" :  
  224.                     ((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion;  
  225.             }  
  226.         }  
  227.     }  
  228. }  
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.
  1. /// <summary>  
  2.         /// Gets the current version of XYZ Assembly, XYZ will be your installed application name which is mentioned in ModifyRegistry.cs class
  3.         /// </summary>  
  4.         /// <returns></returns>  
  5.         private string CurrentRegistryVersion()  
  6.         {  
  7.             ModifyRegistry mr = new ModifyRegistry();  
  8.             string currRegistry = mr.Read("InstallVersion");  
  9.             return currRegistry;  
  10.         }  
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.. :)