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