Advertisement
PiToLoKo

RegEdit written in VB.Net by Elektro

Mar 18th, 2015
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 96.48 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 18-March-2015
  4. ' ***********************************************************************
  5. ' <copyright file="RegEdit.vb" company="Elektro Studios">
  6. '     Copyright (c) Elektro Studios. All rights reserved.
  7. ' </copyright>
  8. ' ***********************************************************************
  9.  
  10. #Region " Usage Examples "
  11.  
  12. ' ----------------
  13. ' Instance RegInfo
  14. ' ----------------
  15. '
  16. '    Dim regInfo As New RegEdit.RegInfo
  17. '    With regInfo
  18. '        .RootKeyName = "HKCU"
  19. '        .SubKeyPath = "Subkey Path"
  20. '        .ValueName = "Value Name"
  21. '        .ValueType = Microsoft.Win32.RegistryValueKind.String
  22. '        .ValueData = "Hello World!"
  23. '    End With
  24. '
  25. '    Dim regInfoByte As New RegEdit.RegInfo(Of Byte())
  26. '    With regInfoByte
  27. '        .RootKeyName = "HKCU"
  28. '        .SubKeyPath = "Subkey Path"
  29. '        .ValueName = "Value Name"
  30. '        .ValueType = Microsoft.Win32.RegistryValueKind.Binary
  31. '        .ValueData = System.Text.Encoding.ASCII.GetBytes("Hello World!")
  32. '    End With
  33.  
  34. ' ----------------
  35. ' Create SubKey
  36. ' ----------------
  37. '
  38. '    RegEdit.CreateSubKey(fullKeyPath:="HKCU\Subkey Path\")
  39. '    RegEdit.CreateSubKey(rootKeyName:="HKCU",
  40. '                         subKeyPath:="Subkey Path")
  41. '    RegEdit.CreateSubKey(regInfo:=regInfoByte)
  42. '
  43. '    Dim regKey1 As Microsoft.Win32.RegistryKey =
  44. '        RegEdit.CreateSubKey(fullKeyPath:="HKCU\Subkey Path\",
  45. '                             registryKeyPermissionCheck:=Microsoft.Win32.RegistryKeyPermissionCheck.Default,
  46. '                             registryOptions:=Microsoft.Win32.RegistryOptions.None)
  47. '
  48. '    Dim regKey2 As Microsoft.Win32.RegistryKey =
  49. '        RegEdit.CreateSubKey(rootKeyName:="HKCU",
  50. '                             subKeyPath:="Subkey Path",
  51. '                             registryKeyPermissionCheck:=Microsoft.Win32.RegistryKeyPermissionCheck.Default,
  52. '                             registryOptions:=Microsoft.Win32.RegistryOptions.None)
  53. '
  54. '    Dim regInfo2 As RegEdit.RegInfo(Of String) = RegEdit.CreateSubKey(Of String)(fullKeyPath:="HKCU\Subkey Path\")
  55. '    Dim regInfo3 As RegEdit.RegInfo(Of String) = RegEdit.CreateSubKey(Of String)(rootKeyName:="HKCU",
  56. '                                                                                 subKeyPath:="Subkey Path")
  57.  
  58. ' ----------------
  59. ' Create Value
  60. ' ----------------
  61. '
  62. '    RegEdit.CreateValue(fullKeyPath:="HKCU\Subkey Path\",
  63. '                        valueName:="Value Name",
  64. '                        valueData:="Value Data",
  65. '                        valueType:=Microsoft.Win32.RegistryValueKind.String)
  66. '
  67. '    RegEdit.CreateValue(rootKeyName:="HKCU",
  68. '                        subKeyPath:="Subkey Path",
  69. '                        valueName:="Value Name",
  70. '                        valueData:="Value Data",
  71. '                        valueType:=Microsoft.Win32.RegistryValueKind.String)
  72. '
  73. '    RegEdit.CreateValue(regInfo:=regInfoByte)
  74. '
  75. '    RegEdit.CreateValue(Of String)(fullKeyPath:="HKCU\Subkey Path\",
  76. '                                   valueName:="Value Name",
  77. '                                   valueData:="Value Data",
  78. '                                   valueType:=Microsoft.Win32.RegistryValueKind.String)
  79. '
  80. '    RegEdit.CreateValue(Of String)(rootKeyName:="HKCU",
  81. '                                   subKeyPath:="Subkey Path",
  82. '                                   valueName:="Value Name",
  83. '                                   valueData:="Value Data",
  84. '                                   valueType:=Microsoft.Win32.RegistryValueKind.String)
  85. '
  86. '    RegEdit.CreateValue(Of Byte())(regInfo:=regInfoByte)
  87.  
  88. ' ----------------
  89. ' Copy KeyTree
  90. ' ----------------
  91. '
  92. '    RegEdit.CopyKeyTree(sourceFullKeyPath:="HKCU\Source Subkey Path\",
  93. '                        targetFullKeyPath:="HKCU\Target Subkey Path\")
  94. '
  95. '    RegEdit.CopyKeyTree(sourceRootKeyName:="HKCU",
  96. '                        sourceSubKeyPath:="Source Subkey Path\",
  97. '                        targetRootKeyName:="HKCU",
  98. '                        targetSubKeyPath:="Target Subkey Path\")
  99.  
  100. ' ----------------
  101. ' Move KeyTree
  102. ' ----------------
  103. '
  104. '    RegEdit.MoveKeyTree(sourceFullKeyPath:="HKCU\Source Subkey Path\",
  105. '                        targetFullKeyPath:="HKCU\Target Subkey Path\")
  106. '
  107. '    RegEdit.MoveKeyTree(sourceRootKeyName:="HKCU",
  108. '                        sourceSubKeyPath:="Source Subkey Path\",
  109. '                        targetRootKeyName:="HKCU",
  110. '                        targetSubKeyPath:="Target Subkey Path\")
  111.  
  112. ' ----------------
  113. ' Copy SubKeys
  114. ' ----------------
  115. '
  116. '    RegEdit.CopySubKeys(sourceFullKeyPath:="HKCU\Source Subkey Path\",
  117. '                        targetFullKeyPath:="HKCU\Target Subkey Path\")
  118. '
  119. '    RegEdit.CopySubKeys(sourceRootKeyName:="HKCU",
  120. '                        sourceSubKeyPath:="Source Subkey Path\",
  121. '                        targetRootKeyName:="HKCU",
  122. '                        targetSubKeyPath:="Target Subkey Path\")
  123.  
  124. ' ----------------
  125. ' Move SubKeys
  126. ' ----------------
  127. '
  128. '    RegEdit.MoveSubKeys(sourceFullKeyPath:="HKCU\Source Subkey Path\",
  129. '                        targetFullKeyPath:="HKCU\Target Subkey Path\")
  130. '
  131. '    RegEdit.MoveSubKeys(sourceRootKeyName:="HKCU",
  132. '                        sourceSubKeyPath:="Source Subkey Path\",
  133. '                        targetRootKeyName:="HKCU",
  134. '                        targetSubKeyPath:="Target Subkey Path\")
  135.  
  136. ' ----------------
  137. ' Copy Value
  138. ' ----------------
  139. '
  140. '    RegEdit.CopyValue(sourceFullKeyPath:="HKCU\Source Subkey Path\",
  141. '                      sourceValueName:="Value Name",
  142. '                      targetFullKeyPath:="HKCU\Target Subkey Path\",
  143. '                      targetValueName:="Value Name")
  144. '
  145. '    RegEdit.CopyValue(sourceRootKeyName:="HKCU",
  146. '                      sourceSubKeyPath:="Source Subkey Path\",
  147. '                      sourceValueName:="Value Name",
  148. '                      targetRootKeyName:="HKCU",
  149. '                      targetSubKeyPath:="Target Subkey Path\",
  150. '                      targetValueName:="Value Name")
  151.  
  152. ' ----------------
  153. ' Move Value
  154. ' ----------------
  155. '
  156. '    RegEdit.MoveValue(sourceFullKeyPath:="HKCU\Source Subkey Path\",
  157. '                      sourceValueName:="Value Name",
  158. '                      targetFullKeyPath:="HKCU\Target Subkey Path\",
  159. '                      targetValueName:="Value Name")
  160. '
  161. '    RegEdit.MoveValue(sourceRootKeyName:="HKCU",
  162. '                      sourceSubKeyPath:="Source Subkey Path\",
  163. '                      sourceValueName:="Value Name",
  164. '                      targetRootKeyName:="HKCU",
  165. '                      targetSubKeyPath:="Target Subkey Path\",
  166. '                      targetValueName:="Value Name")
  167.  
  168. ' ----------------
  169. ' DeleteValue
  170. ' ----------------
  171. '
  172. '    RegEdit.DeleteValue(fullKeyPath:="HKCU\Subkey Path\",
  173. '                        valueName:="Value Name",
  174. '                        throwOnMissingValue:=True)
  175. '
  176. '    RegEdit.DeleteValue(rootKeyName:="HKCU",
  177. '                        subKeyPath:="Subkey Path",
  178. '                        valueName:="Value Name",
  179. '                        throwOnMissingValue:=True)
  180. '
  181. '    RegEdit.DeleteValue(regInfo:=regInfoByte,
  182. '                        throwOnMissingValue:=True)
  183.  
  184. ' ----------------
  185. ' Delete SubKey
  186. ' ----------------
  187. '
  188. '    RegEdit.DeleteSubKey(fullKeyPath:="HKCU\Subkey Path\",
  189. '                         throwOnMissingSubKey:=False)
  190. '
  191. '    RegEdit.DeleteSubKey(rootKeyName:="HKCU",
  192. '                         subKeyPath:="Subkey Path",
  193. '                         throwOnMissingSubKey:=False)
  194. '
  195. '    RegEdit.DeleteSubKey(regInfo:=regInfoByte,
  196. '                         throwOnMissingSubKey:=False)
  197.  
  198. ' ----------------
  199. ' Exist SubKey?
  200. ' ----------------
  201. '
  202. '    Dim exist1 As Boolean = RegEdit.ExistSubKey(fullKeyPath:="HKCU\Subkey Path\")
  203. '
  204. '    Dim exist2 As Boolean = RegEdit.ExistSubKey(rootKeyName:="HKCU",
  205. '                                                subKeyPath:="Subkey Path")
  206.  
  207. ' ----------------
  208. ' Exist Value?
  209. ' ----------------
  210. '
  211. '    Dim exist3 As Boolean = RegEdit.ExistValue(fullKeyPath:="HKCU\Subkey Path\",
  212. '                                               valueName:="Value Name")
  213. '
  214. '    Dim exist4 As Boolean = RegEdit.ExistValue(rootKeyName:="HKCU",
  215. '                                               subKeyPath:="Subkey Path",
  216. '                                               valueName:="Value Name")
  217.  
  218. ' ----------------
  219. ' Value Is Empty?
  220. ' ----------------
  221. '
  222. '    Dim isEmpty1 As Boolean = RegEdit.ValueIsEmpty(fullKeyPath:="HKCU\Subkey Path\",
  223. '                                                   valueName:="Value Name")
  224. '
  225. '    Dim isEmpty2 As Boolean = RegEdit.ValueIsEmpty(rootKeyName:="HKCU",
  226. '                                                   subKeyPath:="Subkey Path",
  227. '                                                   valueName:="Value Name")
  228.  
  229. ' ----------------
  230. ' Export Key
  231. ' ----------------
  232. '
  233. '    RegEdit.ExportKey(fullKeyPath:="HKCU\Subkey Path\",
  234. '                      outputFile:="C:\Backup.reg")
  235. '
  236. '    RegEdit.ExportKey(rootKeyName:="HKCU",
  237. '                      subKeyPath:="Subkey Path",
  238. '                      outputFile:="C:\Backup.reg")
  239.  
  240. ' ----------------
  241. ' Import RegFile
  242. ' ----------------
  243. '
  244. '    RegEdit.ImportRegFile(regFilePath:="C:\Backup.reg")
  245.  
  246. ' ----------------
  247. ' Jump To Key
  248. ' ----------------
  249. '
  250. '    RegEdit.JumpToKey(fullKeyPath:="HKCU\Subkey Path\")
  251. '
  252. '    RegEdit.JumpToKey(rootKeyName:="HKCU",
  253. '                      subKeyPath:="Subkey Path")
  254.  
  255. ' ----------------
  256. ' Find SubKey
  257. ' ----------------
  258. '
  259. '    Dim regInfoSubkeyCol As IEnumerable(Of RegEdit.Reginfo) =
  260. '        RegEdit.FindSubKey(rootKeyName:="HKCU",
  261. '                           subKeyPath:="Subkey Path",
  262. '                           subKeyName:="Subkey Name",
  263. '                           matchFullSubKeyName:=False,
  264. '                           ignoreCase:=True,
  265. '                           searchOption:=IO.SearchOption.AllDirectories)
  266. '
  267. '    For Each reg As RegEdit.RegInfo In regInfoSubkeyCol
  268. '        Debug.WriteLine(reg.RootKeyName)
  269. '        Debug.WriteLine(reg.SubKeyPath)
  270. '        Debug.WriteLine(reg.ValueName)
  271. '        Debug.WriteLine(reg.ValueData.ToString)
  272. '        Debug.WriteLine("")
  273. '    Next reg
  274.  
  275. ' ----------------
  276. ' Find Value
  277. ' ----------------
  278. '
  279. '    Dim regInfoValueNameCol As IEnumerable(Of RegEdit.Reginfo) =
  280. '        RegEdit.FindValue(rootKeyName:="HKCU",
  281. '                              subKeyPath:="Subkey Path",
  282. '                              valueName:="Value Name",
  283. '                              matchFullValueName:=False,
  284. '                              ignoreCase:=True,
  285. '                              searchOption:=IO.SearchOption.AllDirectories)
  286. '
  287. '    For Each reg As RegEdit.RegInfo In regInfoValueNameCol
  288. '        Debug.WriteLine(reg.RootKeyName)
  289. '        Debug.WriteLine(reg.SubKeyPath)
  290. '        Debug.WriteLine(reg.ValueName)
  291. '        Debug.WriteLine(reg.ValueData.ToString)
  292. '        Debug.WriteLine("")
  293. '    Next reg
  294.  
  295. ' ----------------
  296. ' Find Value Data
  297. ' ----------------
  298. '
  299. '    Dim regInfoValueDataCol As IEnumerable(Of RegEdit.Reginfo) =
  300. '        RegEdit.FindValueData(rootKeyName:="HKCU",
  301. '                              subKeyPath:="Subkey Path",
  302. '                              valueData:="Value Data",
  303. '                              matchFullData:=False,
  304. '                              ignoreCase:=True,
  305. '                              searchOption:=IO.SearchOption.AllDirectories)
  306. '
  307. '    For Each reg As RegEdit.RegInfo In regInfoValueDataCol
  308. '        Debug.WriteLine(reg.RootKeyName)
  309. '        Debug.WriteLine(reg.SubKeyPath)
  310. '        Debug.WriteLine(reg.ValueName)
  311. '        Debug.WriteLine(reg.ValueData.ToString)
  312. '        Debug.WriteLine("")
  313. '    Next reg
  314.  
  315. ' ----------------
  316. ' Get...
  317. ' ----------------
  318. '
  319. '    Dim rootKeyName As String = RegEdit.GetRootKeyName(registryPath:="HKCU\Subkey Path\")
  320. '    Dim subKeyPath As String = RegEdit.GetSubKeyPath(registryPath:="HKCU\Subkey Path\")
  321. '    Dim rootKey As Microsoft.Win32.RegistryKey = RegEdit.GetRootKey(registryPath:="HKCU\Subkey Path\")
  322.  
  323. ' ----------------
  324. ' Get Value Data
  325. ' ----------------
  326. '
  327. '    Dim dataObject As Object = RegEdit.GetValueData(rootKeyName:="HKCU",
  328. '                                                    subKeyPath:="Subkey Path",
  329. '                                                    valueName:="Value Name")
  330. '
  331. '    Dim dataString As String = RegEdit.GetValueData(Of String)(fullKeyPath:="HKCU\Subkey Path\",
  332. '                                                               valueName:="Value Name",
  333. '                                                               registryValueOptions:=Microsoft.Win32.RegistryValueOptions.DoNotExpandEnvironmentNames)
  334. '
  335. '    Dim dataByte As Byte() = RegEdit.GetValueData(Of Byte())(regInfo:=regInfoByte,
  336. '                                                             registryValueOptions:=Microsoft.Win32.RegistryValueOptions.None)
  337. '    Debug.WriteLine("dataByte=" & String.Join(",", dataByte))
  338.  
  339. ' -----------------
  340. ' Set UserAccessKey
  341. ' -----------------
  342. '
  343. 'RegEdit.SetUserAccessKey(fullKeyPath:="HKCU\Subkey Path",
  344. '                         userAccess:={RegEdit.ReginiUserAccess.AdministratorsFullAccess})
  345. '
  346. 'RegEdit.SetUserAccessKey(rootKeyName:="HKCU",
  347. '                         subKeyPath:="Subkey Path",
  348. '                         userAccess:={RegEdit.ReginiUserAccess.AdministratorsFullAccess,
  349. '                                      RegEdit.ReginiUserAccess.CreatorFullAccess,
  350. '                                      RegEdit.ReginiUserAccess.SystemFullAccess})
  351.  
  352. #End Region
  353.  
  354. #Region " Imports "
  355.  
  356. Imports Microsoft.Win32
  357. Imports System.IO
  358. Imports System.Security.AccessControl
  359. Imports System.Text
  360.  
  361. #End Region
  362.  
  363. #Region " RegEdit "
  364.  
  365. ''' <summary>
  366. ''' Contains registry related methods.
  367. ''' </summary>
  368. Public NotInheritable Class RegEdit
  369.  
  370. #Region " Enumerations "
  371.  
  372.     ''' <summary>
  373.     ''' Specifies an user identifier for Regini.exe application.
  374.     ''' </summary>
  375.     Public Enum ReginiUserAccess As Integer
  376.  
  377.         AdministratorsFullAccess = 1
  378.  
  379.         AdministratorsReadAccess = 2
  380.  
  381.         AdministratorsReadAndWriteAccess = 3
  382.  
  383.         AdministratorsReadWriteAndDeleteAccess = 4
  384.  
  385.         AdministratorsReadWriteAndExecuteAccess = 20
  386.  
  387.         CreatorFullAccess = 5
  388.  
  389.         CreatorReadAndWriteAccess = 6
  390.  
  391.         InteractiveUserFullAccess = 21
  392.  
  393.         InteractiveUserReadAndWriteAccess = 22
  394.  
  395.         InteractiveUserReadWriteAndDeleteAccess = 23
  396.  
  397.         PowerUsersFullAccess = 11
  398.  
  399.         PowerUsersReadAndWriteAccess = 12
  400.  
  401.         PowerUsersReadWriteAndDeleteAccess = 13
  402.  
  403.         SystemFullAccess = 17
  404.  
  405.         SystemOperatorsFullAccess = 14
  406.  
  407.         SystemOperatorsReadAndWriteAccess = 15
  408.  
  409.         SystemOperatorsReadWriteAndDeleteAccess = 16
  410.  
  411.         SystemReadAccess = 19
  412.  
  413.         SystemReadAndWriteAccess = 18
  414.  
  415.         WorldFullAccess = 7
  416.  
  417.         WorldReadAccess = 8
  418.  
  419.         WorldReadAndWriteAccess = 9
  420.  
  421.         WorldReadWriteAndDeleteAccess = 10
  422.  
  423.     End Enum
  424.  
  425. #End Region
  426.  
  427. #Region " Types "
  428.  
  429. #Region " RegInfo(Of T) "
  430.  
  431.     ''' <summary>
  432.     ''' Defines a registry key with a specified type of data.
  433.     ''' </summary>
  434.     Public Class RegInfo(Of T)
  435.  
  436. #Region " Properties "
  437.  
  438.         ''' <summary>
  439.         ''' Gets or sets the registry root key.
  440.         ''' ( eg: HKCU or HKEY_CURRENT_USER)
  441.         ''' </summary>
  442.         ''' <value>The registry root key.</value>
  443.         Public Property RootKeyName As String
  444.  
  445.         ''' <summary>
  446.         ''' Gets or sets the registry subkey path.
  447.         ''' ( eg: subkey1\subkey2\ )
  448.         ''' </summary>
  449.         ''' <value>The registry subkey path.</value>
  450.         Public Property SubKeyPath As String
  451.  
  452.         ''' <summary>
  453.         ''' Gets or sets the registry value name.
  454.         ''' </summary>
  455.         ''' <value>The registry value name.</value>
  456.         Public Property ValueName As String
  457.  
  458.         ''' <summary>
  459.         ''' Gets or sets the type of the registry value.
  460.         ''' </summary>
  461.         ''' <value>The type of the registry value.</value>
  462.         Public Property ValueType As RegistryValueKind = RegistryValueKind.Unknown
  463.  
  464.         ''' <summary>
  465.         ''' Gets or sets the data of the registry value.
  466.         ''' </summary>
  467.         ''' <value>The data of the registry value.</value>
  468.         Public Overridable Property ValueData As T
  469.  
  470.         ''' <summary>
  471.         ''' Gets the full key path.
  472.         ''' </summary>
  473.         ''' <value>The full key path.</value>
  474.         Public ReadOnly Property FullKeyPath As String
  475.             Get
  476.                 Return String.Format("{0}\{1}", RegEdit.GetRootKeyName(Me.RootKeyName), RegEdit.GetSubKeyPath(Me.SubKeyPath))
  477.             End Get
  478.         End Property
  479.  
  480.         ''' <summary>
  481.         ''' Gets a <see cref="RegistryKey"/> instance of the current RootKey\SubKey.
  482.         ''' </summary>
  483.         ''' <value>A <see cref="RegistryKey"/> instance of the current RootKey\SubKey.</value>
  484.         Public ReadOnly Property RegistryKey(Optional ByVal registryKeyPermissionCheck As RegistryKeyPermissionCheck =
  485.                                                             RegistryKeyPermissionCheck.Default,
  486.                                              Optional ByVal registryOptions As RegistryRights =
  487.                                                             RegistryRights.CreateSubKey) As RegistryKey
  488.             Get
  489.                 Return RegEdit.GetRootKey(Me.RootKeyName).OpenSubKey(Me.SubKeyPath, registryKeyPermissionCheck, registryOptions)
  490.             End Get
  491.  
  492.         End Property
  493.  
  494. #End Region
  495.  
  496.     End Class
  497.  
  498. #End Region
  499.  
  500. #Region " RegInfo "
  501.  
  502.     ''' <summary>
  503.     ''' Defines a registry key.
  504.     ''' </summary>
  505.     Public NotInheritable Class RegInfo : Inherits RegInfo(Of Object)
  506.  
  507. #Region " Properties "
  508.  
  509.         ''' <summary>
  510.         ''' Gets or sets the data of the registry value.
  511.         ''' </summary>
  512.         ''' <value>The data of the registry value.</value>
  513.         Public Overrides Property ValueData As Object
  514.  
  515. #End Region
  516.  
  517.     End Class
  518.  
  519. #End Region
  520.  
  521. #End Region
  522.  
  523. #Region " Public Methods "
  524.  
  525. #Region " Create SubKey "
  526.  
  527.     ''' <summary>
  528.     ''' Creates a new registry subkey.
  529.     ''' </summary>
  530.     ''' <typeparam name="T"></typeparam>
  531.     ''' <param name="rootKeyName">The rootkey name.</param>
  532.     ''' <param name="subKeyPath">The subkey path.</param>
  533.     ''' <param name="registryKeyPermissionCheck">The registry key permission check.</param>
  534.     ''' <param name="registryOptions">The registry options.</param>
  535.     ''' <returns>The registry key.</returns>
  536.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath</exception>
  537.     Public Shared Function CreateSubKey(Of T)(ByVal rootKeyName As String,
  538.                                               ByVal subKeyPath As String,
  539.                                               Optional ByVal registryKeyPermissionCheck As RegistryKeyPermissionCheck =
  540.                                                              RegistryKeyPermissionCheck.Default,
  541.                                               Optional ByVal registryOptions As RegistryOptions =
  542.                                                              RegistryOptions.None) As RegInfo(Of T)
  543.  
  544.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  545.             Throw New ArgumentNullException("rootKeyName")
  546.  
  547.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  548.             Throw New ArgumentNullException("subKeyPath")
  549.  
  550.         Else
  551.             Dim reg As RegistryKey = Nothing
  552.             Try
  553.  
  554.                 Dim regInfo As New RegInfo(Of T)
  555.                 With regInfo
  556.                     .RootKeyName = GetRootKeyName(rootKeyName)
  557.                     .SubKeyPath = GetSubKeyPath(subKeyPath)
  558.                 End With
  559.  
  560.                 reg = GetRootKey(rootKeyName)
  561.                 Using reg
  562.                     reg.CreateSubKey(GetSubKeyPath(subKeyPath), registryKeyPermissionCheck, registryOptions)
  563.                 End Using
  564.  
  565.                 Return regInfo
  566.  
  567.             Catch ex As Exception
  568.                 Throw
  569.  
  570.             Finally
  571.                 If reg IsNot Nothing Then
  572.                     reg.Close()
  573.                 End If
  574.  
  575.             End Try
  576.  
  577.         End If
  578.  
  579.     End Function
  580.  
  581.     ''' <summary>
  582.     ''' Creates a new registry subkey.
  583.     ''' </summary>
  584.     ''' <typeparam name="T"></typeparam>
  585.     ''' <param name="fullKeyPath">The registry key full path.</param>
  586.     ''' <param name="registryKeyPermissionCheck">The registry key permission check.</param>
  587.     ''' <param name="registryOptions">The registry options.</param>
  588.     ''' <returns>The registry key.</returns>
  589.     Public Shared Function CreateSubKey(Of T)(ByVal fullKeyPath As String,
  590.                                               Optional ByVal registryKeyPermissionCheck As RegistryKeyPermissionCheck =
  591.                                                              RegistryKeyPermissionCheck.Default,
  592.                                               Optional ByVal registryOptions As RegistryOptions =
  593.                                                              RegistryOptions.None) As RegInfo(Of T)
  594.  
  595.         Return CreateSubKey(Of T)(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath), registryKeyPermissionCheck, registryOptions)
  596.  
  597.     End Function
  598.  
  599.     ''' <summary>
  600.     ''' Creates a new registry subkey.
  601.     ''' </summary>
  602.     ''' <typeparam name="T"></typeparam>
  603.     ''' <param name="regInfo">A <see cref="Regedit.RegInfo(Of T)"/> instance containing the registry info.</param>
  604.     ''' <param name="registryKeyPermissionCheck">The registry key permission check.</param>
  605.     ''' <param name="registryOptions">The registry options.</param>
  606.     ''' <returns>The registry key.</returns>
  607.     Public Shared Function CreateSubKey(Of T)(ByVal regInfo As RegInfo(Of T),
  608.                                               Optional ByVal registryKeyPermissionCheck As RegistryKeyPermissionCheck =
  609.                                                              RegistryKeyPermissionCheck.Default,
  610.                                               Optional ByVal registryOptions As RegistryOptions =
  611.                                                              RegistryOptions.None) As RegInfo(Of T)
  612.  
  613.         Return CreateSubKey(Of T)(regInfo.RootKeyName, regInfo.SubKeyPath, registryKeyPermissionCheck, registryOptions)
  614.  
  615.     End Function
  616.  
  617.     ''' <summary>
  618.     ''' Creates a new registry subkey.
  619.     ''' </summary>
  620.     ''' <param name="rootKeyName">The rootkey name.</param>
  621.     ''' <param name="subKeyPath">The subkey path.</param>
  622.     ''' <param name="registryKeyPermissionCheck">The registry key permission check.</param>
  623.     ''' <param name="registryOptions">The registry options.</param>
  624.     ''' <returns>The registry key.</returns>
  625.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath</exception>
  626.     Public Shared Function CreateSubKey(ByVal rootKeyName As String,
  627.                                         ByVal subKeyPath As String,
  628.                                         Optional ByVal registryKeyPermissionCheck As RegistryKeyPermissionCheck =
  629.                                                        RegistryKeyPermissionCheck.Default,
  630.                                         Optional ByVal registryOptions As RegistryOptions =
  631.                                                        RegistryOptions.None) As RegistryKey
  632.  
  633.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  634.             Throw New ArgumentNullException("rootKeyName")
  635.  
  636.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  637.             Throw New ArgumentNullException("subKeyPath")
  638.  
  639.         Else
  640.             Dim reg As RegistryKey = Nothing
  641.             Try
  642.                 reg = GetRootKey(rootKeyName)
  643.                 Using reg
  644.                     Return reg.CreateSubKey(GetSubKeyPath(subKeyPath), registryKeyPermissionCheck, registryOptions)
  645.                 End Using
  646.  
  647.             Catch ex As Exception
  648.                 Throw
  649.  
  650.             Finally
  651.                 If reg IsNot Nothing Then
  652.                     reg.Close()
  653.                 End If
  654.  
  655.             End Try
  656.  
  657.         End If
  658.  
  659.     End Function
  660.  
  661.     ''' <summary>
  662.     ''' Creates a new registry subkey.
  663.     ''' </summary>
  664.     ''' <param name="fullKeyPath">The registry key full path.</param>
  665.     ''' <param name="registryKeyPermissionCheck">The registry key permission check.</param>
  666.     ''' <param name="registryOptions">The registry options.</param>
  667.     ''' <returns>The registry key.</returns>
  668.     Public Shared Function CreateSubKey(ByVal fullKeyPath As String,
  669.                                         Optional ByVal registryKeyPermissionCheck As RegistryKeyPermissionCheck =
  670.                                                        RegistryKeyPermissionCheck.Default,
  671.                                         Optional ByVal registryOptions As RegistryOptions =
  672.                                                        RegistryOptions.None) As RegistryKey
  673.  
  674.         Return CreateSubKey(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath), registryKeyPermissionCheck, registryOptions)
  675.  
  676.     End Function
  677.  
  678.     ''' <summary>
  679.     ''' Creates a new registry subkey.
  680.     ''' </summary>
  681.     ''' <param name="regInfo">A <see cref="Regedit.RegInfo(Of T)"/> instance containing the registry info.</param>
  682.     ''' <param name="registryKeyPermissionCheck">The registry key permission check.</param>
  683.     ''' <param name="registryOptions">The registry options.</param>
  684.     ''' <returns>The registry key.</returns>
  685.     Public Shared Function CreateSubKey(ByVal regInfo As RegInfo,
  686.                                         Optional ByVal registryKeyPermissionCheck As RegistryKeyPermissionCheck =
  687.                                                        RegistryKeyPermissionCheck.Default,
  688.                                         Optional ByVal registryOptions As RegistryOptions =
  689.                                                        RegistryOptions.None) As RegistryKey
  690.  
  691.         Return CreateSubKey(regInfo.RootKeyName, regInfo.SubKeyPath, registryKeyPermissionCheck, registryOptions)
  692.  
  693.     End Function
  694.  
  695. #End Region
  696.  
  697. #Region " Delete SubKey "
  698.  
  699.     ''' <summary>
  700.     ''' Deletes a registry subkey.
  701.     ''' </summary>
  702.     ''' <param name="rootKeyName">The rootkey name.</param>
  703.     ''' <param name="subKeyPath">The subkey path.</param>
  704.     ''' <param name="throwOnMissingSubKey">If set to <c>true</c>, throws an exception on missing subkey.</param>
  705.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath</exception>
  706.     Public Shared Sub DeleteSubKey(ByVal rootKeyName As String,
  707.                                    ByVal subKeyPath As String,
  708.                                    Optional ByVal throwOnMissingSubKey As Boolean = False)
  709.  
  710.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  711.             Throw New ArgumentNullException("rootKeyName")
  712.  
  713.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  714.             Throw New ArgumentNullException("subKeyPath")
  715.  
  716.         Else
  717.             Dim reg As RegistryKey = Nothing
  718.             Try
  719.                 reg = GetRootKey(rootKeyName)
  720.                 Using reg
  721.                     reg.DeleteSubKeyTree(GetSubKeyPath(subKeyPath), throwOnMissingSubKey)
  722.                 End Using
  723.  
  724.             Catch ex As Exception
  725.                 Throw
  726.  
  727.             Finally
  728.                 If reg IsNot Nothing Then
  729.                     reg.Close()
  730.                 End If
  731.  
  732.             End Try
  733.  
  734.         End If
  735.  
  736.     End Sub
  737.  
  738.     ''' <summary>
  739.     ''' Deletes a registry subkey.
  740.     ''' </summary>
  741.     ''' <param name="fullKeyPath">The registry key full path.</param>
  742.     ''' <param name="throwOnMissingSubKey">If set to <c>true</c>, throws an exception on missing subkey.</param>
  743.     Public Shared Sub DeleteSubKey(ByVal fullKeyPath As String,
  744.                                    Optional ByVal throwOnMissingSubKey As Boolean = False)
  745.  
  746.         DeleteSubKey(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath), throwOnMissingSubKey)
  747.  
  748.     End Sub
  749.  
  750.     ''' <summary>
  751.     ''' Deletes a registry subkey.
  752.     ''' </summary>
  753.     ''' <typeparam name="T"></typeparam>
  754.     ''' <param name="regInfo">A <see cref="Regedit.RegInfo(of T)"/> instance containing the registry info.</param>
  755.     ''' <param name="throwOnMissingSubKey">If set to <c>true</c>, throws an exception on missing subkey.</param>
  756.     Public Shared Sub DeleteSubKey(Of T)(ByVal regInfo As RegInfo(Of T),
  757.                                          Optional ByVal throwOnMissingSubKey As Boolean = False)
  758.  
  759.         DeleteSubKey(regInfo.RootKeyName, regInfo.SubKeyPath, throwOnMissingSubKey)
  760.  
  761.     End Sub
  762.  
  763. #End Region
  764.  
  765. #Region " Create Value "
  766.  
  767.     ''' <summary>
  768.     ''' Creates or replaces a registry value.
  769.     ''' </summary>
  770.     ''' <typeparam name="T"></typeparam>
  771.     ''' <param name="rootKeyName">The rootkey name.</param>
  772.     ''' <param name="subKeyPath">The subkey path.</param>
  773.     ''' <param name="valueName">The value name.</param>
  774.     ''' <param name="valueData">The value data.</param>
  775.     ''' <param name="valueType">The registry value type.</param>
  776.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath or valueName</exception>
  777.     Public Shared Sub CreateValue(Of T)(ByVal rootKeyName As String,
  778.                                         ByVal subKeyPath As String,
  779.                                         ByVal valueName As String,
  780.                                         ByVal valueData As T,
  781.                                         Optional ByVal valueType As RegistryValueKind = RegistryValueKind.String)
  782.  
  783.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  784.             Throw New ArgumentNullException("rootKeyName")
  785.  
  786.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  787.             Throw New ArgumentNullException("subKeyPath")
  788.  
  789.         ElseIf String.IsNullOrEmpty(valueName) OrElse String.IsNullOrWhiteSpace(valueName) Then
  790.             Throw New ArgumentNullException("valueName")
  791.  
  792.         Else
  793.             Dim reg As RegistryKey = Nothing
  794.             Try
  795.                 reg = GetRootKey(rootKeyName)
  796.                 Using reg
  797.                     reg.OpenSubKey(GetSubKeyPath(subKeyPath), writable:=True).SetValue(valueName, valueData, valueType)
  798.                 End Using
  799.  
  800.             Catch ex As Exception
  801.                 Throw
  802.  
  803.             Finally
  804.                 If reg IsNot Nothing Then
  805.                     reg.Close()
  806.                 End If
  807.  
  808.             End Try
  809.  
  810.         End If
  811.  
  812.     End Sub
  813.  
  814.     ''' <summary>
  815.     ''' Creates or replaces a registry value.
  816.     ''' </summary>
  817.     ''' <typeparam name="T"></typeparam>
  818.     ''' <param name="fullKeyPath">The registry key full path.</param>
  819.     ''' <param name="valueName">The value name.</param>
  820.     ''' <param name="valueData">The value data.</param>
  821.     ''' <param name="valueType">The registry value type.</param>
  822.     Public Shared Sub CreateValue(Of T)(ByVal fullKeyPath As String,
  823.                                         ByVal valueName As String,
  824.                                         ByVal valueData As T,
  825.                                         Optional ByVal valueType As RegistryValueKind = RegistryValueKind.String)
  826.  
  827.         CreateValue(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath), valueName, valueData, valueType)
  828.  
  829.     End Sub
  830.  
  831.     ''' <summary>
  832.     ''' Creates or replaces a registry value.
  833.     ''' </summary>
  834.     ''' <typeparam name="T"></typeparam>
  835.     ''' <param name="regInfo">A <see cref="Regedit.RegInfo(Of T)"/> instance containing the registry info.</param>
  836.     Public Shared Sub CreateValue(Of T)(ByVal regInfo As RegInfo(Of T))
  837.  
  838.         CreateValue(regInfo.RootKeyName, regInfo.SubKeyPath, regInfo.ValueName, regInfo.ValueData, regInfo.ValueType)
  839.  
  840.     End Sub
  841.  
  842. #End Region
  843.  
  844. #Region " Delete Value "
  845.  
  846.     ''' <summary>
  847.     ''' Deletes a registry subkey.
  848.     ''' </summary>
  849.     ''' <param name="rootKeyName">The rootkey name.</param>
  850.     ''' <param name="subKeyPath">The subkey path.</param>
  851.     ''' <param name="valueName">The value name.</param>
  852.     ''' <param name="throwOnMissingValue">If set to <c>true</c>, throws an exception on missing value.</param>
  853.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath or valueName</exception>
  854.     Public Shared Sub DeleteValue(ByVal rootKeyName As String,
  855.                                   ByVal subKeyPath As String,
  856.                                   ByVal valueName As String,
  857.                                   Optional ByVal throwOnMissingValue As Boolean = False)
  858.  
  859.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  860.             Throw New ArgumentNullException("rootKeyName")
  861.  
  862.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  863.             Throw New ArgumentNullException("subKeyPath")
  864.  
  865.         ElseIf String.IsNullOrEmpty(valueName) OrElse String.IsNullOrWhiteSpace(valueName) Then
  866.             Throw New ArgumentNullException("valueName")
  867.  
  868.         Else
  869.             Dim reg As RegistryKey = Nothing
  870.             Try
  871.                 reg = GetRootKey(rootKeyName)
  872.                 Using reg
  873.                     reg.OpenSubKey(GetSubKeyPath(subKeyPath), writable:=True).DeleteValue(valueName, throwOnMissingValue)
  874.                 End Using
  875.  
  876.             Catch ex As Exception
  877.                 Throw
  878.  
  879.             Finally
  880.                 If reg IsNot Nothing Then
  881.                     reg.Close()
  882.                 End If
  883.  
  884.             End Try
  885.  
  886.         End If
  887.  
  888.     End Sub
  889.  
  890.     ''' <summary>
  891.     ''' Deletes a registry subkey.
  892.     ''' </summary>
  893.     ''' <param name="fullKeyPath">The registry key full path.</param>
  894.     ''' <param name="valueName">The value name.</param>
  895.     ''' <param name="throwOnMissingValue">If set to <c>true</c>, throws an exception on missing value.</param>
  896.     Public Shared Sub DeleteValue(ByVal fullKeyPath As String,
  897.                                   ByVal valueName As String,
  898.                                   Optional ByVal throwOnMissingValue As Boolean = False)
  899.  
  900.         DeleteValue(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath), valueName, throwOnMissingValue)
  901.  
  902.     End Sub
  903.  
  904.     ''' <summary>
  905.     ''' Deletes a registry subkey.
  906.     ''' </summary>
  907.     ''' <typeparam name="T"></typeparam>
  908.     ''' <param name="regInfo">A <see cref="Regedit.RegInfo(of T)"/> instance containing the registry info.</param>
  909.     ''' <param name="throwOnMissingValue">If set to <c>true</c>, throws an exception on missing value.</param>
  910.     Public Shared Sub DeleteValue(Of T)(ByVal regInfo As RegInfo(Of T),
  911.                                         Optional ByVal throwOnMissingValue As Boolean = False)
  912.  
  913.         DeleteValue(regInfo.RootKeyName, regInfo.SubKeyPath, regInfo.ValueName, throwOnMissingValue)
  914.  
  915.     End Sub
  916.  
  917. #End Region
  918.  
  919. #Region " Get ValueData "
  920.  
  921.     ''' <summary>
  922.     ''' Gets the data of a registry value.
  923.     ''' </summary>
  924.     ''' <typeparam name="T"></typeparam>
  925.     ''' <param name="rootKeyName">The rootkey name.</param>
  926.     ''' <param name="subKeyPath">The subkey path.</param>
  927.     ''' <param name="valueName">The value name.</param>
  928.     ''' <param name="registryValueOptions">The registry value options.</param>
  929.     ''' <returns>The value data</returns>
  930.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath</exception>
  931.     Public Shared Function GetValueData(Of T)(ByVal rootKeyName As String,
  932.                                               ByVal subKeyPath As String,
  933.                                               ByVal valueName As String,
  934.                                               Optional ByVal registryValueOptions As RegistryValueOptions =
  935.                                                              RegistryValueOptions.None) As T
  936.  
  937.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  938.             Throw New ArgumentNullException("rootKeyName")
  939.  
  940.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  941.             Throw New ArgumentNullException("subKeyPath")
  942.  
  943.         Else
  944.             Dim reg As RegistryKey = Nothing
  945.             Try
  946.                 reg = GetRootKey(rootKeyName)
  947.                 Using reg
  948.                     Return DirectCast(reg.OpenSubKey(GetSubKeyPath(subKeyPath), writable:=False).
  949.                                           GetValue(valueName, defaultValue:=Nothing, options:=registryValueOptions), T)
  950.                 End Using
  951.  
  952.             Catch ex As Exception
  953.                 Throw
  954.  
  955.             Finally
  956.                 If reg IsNot Nothing Then
  957.                     reg.Close()
  958.                 End If
  959.  
  960.             End Try
  961.  
  962.         End If
  963.  
  964.     End Function
  965.  
  966.     ''' <summary>
  967.     ''' Gets the data of a registry value.
  968.     ''' </summary>
  969.     ''' <typeparam name="T"></typeparam>
  970.     ''' <param name="fullKeyPath">The registry key full path.</param>
  971.     ''' <param name="valueName">The value name.</param>
  972.     ''' <param name="registryValueOptions">The registry value options.</param>
  973.     ''' <returns>The value data</returns>
  974.     Public Shared Function GetValueData(Of T)(ByVal fullKeyPath As String,
  975.                                               ByVal valueName As String,
  976.                                               Optional ByVal registryValueOptions As RegistryValueOptions =
  977.                                                              RegistryValueOptions.None) As T
  978.  
  979.         Return GetValueData(Of T)(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath), valueName, registryValueOptions)
  980.  
  981.     End Function
  982.  
  983.     ''' <summary>
  984.     ''' Gets the data of a registry value.
  985.     ''' </summary>
  986.     ''' <typeparam name="T"></typeparam>
  987.     ''' <param name="regInfo">A <see cref="Regedit.RegInfo(of T)"/> instance containing the registry info.</param>
  988.     ''' <param name="registryValueOptions">The registry value options.</param>
  989.     ''' <returns>The value data</returns>
  990.     Public Shared Function GetValueData(Of T)(ByVal regInfo As RegInfo(Of T),
  991.                                               Optional ByVal registryValueOptions As RegistryValueOptions =
  992.                                                              RegistryValueOptions.None) As T
  993.  
  994.         Return GetValueData(Of T)(regInfo.RootKeyName, regInfo.SubKeyPath, regInfo.ValueName, registryValueOptions)
  995.  
  996.     End Function
  997.  
  998.     ''' <summary>
  999.     ''' Gets the data of a registry value.
  1000.     ''' </summary>
  1001.     ''' <param name="rootKeyName">The rootkey name.</param>
  1002.     ''' <param name="subKeyPath">The subkey path.</param>
  1003.     ''' <param name="valueName">The value name.</param>
  1004.     ''' <param name="registryValueOptions">The registry value options.</param>
  1005.     ''' <returns>The value data</returns>
  1006.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath or valueName</exception>
  1007.     Public Shared Function GetValueData(ByVal rootKeyName As String,
  1008.                                               ByVal subKeyPath As String,
  1009.                                               ByVal valueName As String,
  1010.                                               Optional ByVal registryValueOptions As RegistryValueOptions =
  1011.                                                              RegistryValueOptions.None) As Object
  1012.  
  1013.         Return GetValueData(Of Object)(rootKeyName, subKeyPath, valueName, registryValueOptions)
  1014.  
  1015.     End Function
  1016.  
  1017.     ''' <summary>
  1018.     ''' Gets the data of a registry value.
  1019.     ''' </summary>
  1020.     ''' <param name="fullKeyPath">The registry key full path.</param>
  1021.     ''' <param name="valueName">The value name.</param>
  1022.     ''' <param name="registryValueOptions">The registry value options.</param>
  1023.     ''' <returns>The value data</returns>
  1024.     Public Shared Function GetValueData(ByVal fullKeyPath As String,
  1025.                                         ByVal valueName As String,
  1026.                                         Optional ByVal registryValueOptions As RegistryValueOptions =
  1027.                                                        RegistryValueOptions.None) As Object
  1028.  
  1029.         Return GetValueData(Of Object)(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath), valueName, registryValueOptions)
  1030.  
  1031.     End Function
  1032.  
  1033.     ''' <summary>
  1034.     ''' Gets the data of a registry value.
  1035.     ''' </summary>
  1036.     ''' <param name="regInfo">A <see cref="Regedit.RegInfo(of T)"/> instance containing the registry info.</param>
  1037.     ''' <param name="registryValueOptions">The registry value options.</param>
  1038.     ''' <returns>The value data</returns>
  1039.     Public Shared Function GetValueData(ByVal regInfo As RegInfo,
  1040.                                         Optional ByVal registryValueOptions As RegistryValueOptions =
  1041.                                                        RegistryValueOptions.None) As Object
  1042.  
  1043.         Return GetValueData(Of Object)(regInfo.RootKeyName, regInfo.SubKeyPath, regInfo.ValueName, registryValueOptions)
  1044.  
  1045.     End Function
  1046.  
  1047. #End Region
  1048.  
  1049. #Region " Exists ? "
  1050.  
  1051.     ''' <summary>
  1052.     ''' Determines whether a registry subkey exists.
  1053.     ''' </summary>
  1054.     ''' <param name="rootKeyName">The rootkey name.</param>
  1055.     ''' <param name="subKeyPath">The subkey path.</param>
  1056.     ''' <returns><c>true</c> if subkey exist, <c>false</c> otherwise.</returns>
  1057.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath</exception>
  1058.     Public Shared Function ExistSubKey(ByVal rootKeyName As String,
  1059.                                        ByVal subKeyPath As String) As Boolean
  1060.  
  1061.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  1062.             Throw New ArgumentNullException("rootKeyName")
  1063.  
  1064.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  1065.             Throw New ArgumentNullException("subKeyPath")
  1066.  
  1067.         Else
  1068.             Dim reg As RegistryKey = Nothing
  1069.             Try
  1070.                 reg = GetRootKey(rootKeyName)
  1071.                 Using reg
  1072.                     Return reg.OpenSubKey(GetSubKeyPath(subKeyPath), writable:=False) IsNot Nothing
  1073.                 End Using
  1074.  
  1075.             Catch ex As Exception
  1076.                 Throw
  1077.  
  1078.             Finally
  1079.                 If reg IsNot Nothing Then
  1080.                     reg.Close()
  1081.                 End If
  1082.  
  1083.             End Try
  1084.         End If
  1085.  
  1086.     End Function
  1087.  
  1088.     ''' <summary>
  1089.     ''' Determines whether a registry subkey exists.
  1090.     ''' </summary>
  1091.     ''' <param name="fullKeyPath">The registry key full path.</param>
  1092.     ''' <returns><c>true</c> if subkey exist, <c>false</c> otherwise.</returns>
  1093.     Public Shared Function ExistSubKey(ByVal fullKeyPath As String) As Boolean
  1094.  
  1095.         Return ExistSubKey(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath))
  1096.  
  1097.     End Function
  1098.  
  1099.     ''' <summary>
  1100.     ''' Determines whether a registry value exists.
  1101.     ''' </summary>
  1102.     ''' <param name="rootKeyName">The rootkey name.</param>
  1103.     ''' <param name="subKeyPath">The subkey path.</param>
  1104.     ''' <param name="valueName">The value name.</param>
  1105.     ''' <returns><c>true</c> if value exist, <c>false</c> otherwise.</returns>
  1106.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath or valueName</exception>
  1107.     Public Shared Function ExistValue(ByVal rootKeyName As String,
  1108.                                       ByVal subKeyPath As String,
  1109.                                       ByVal valueName As String) As Boolean
  1110.  
  1111.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  1112.             Throw New ArgumentNullException("rootKeyName")
  1113.  
  1114.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  1115.             Throw New ArgumentNullException("subKeyPath")
  1116.  
  1117.         ElseIf String.IsNullOrEmpty(valueName) OrElse String.IsNullOrWhiteSpace(valueName) Then
  1118.             Throw New ArgumentNullException("valueName")
  1119.  
  1120.         Else
  1121.             Dim reg As RegistryKey = Nothing
  1122.             Try
  1123.                 reg = GetRootKey(rootKeyName)
  1124.                 Using reg
  1125.                     Return reg.OpenSubKey(GetSubKeyPath(subKeyPath), writable:=False).
  1126.                                GetValue(valueName, defaultValue:=Nothing) IsNot Nothing
  1127.                 End Using
  1128.  
  1129.             Catch ex As Exception
  1130.                 Throw
  1131.  
  1132.             Finally
  1133.                 If reg IsNot Nothing Then
  1134.                     reg.Close()
  1135.                 End If
  1136.  
  1137.             End Try
  1138.         End If
  1139.  
  1140.     End Function
  1141.  
  1142.     ''' <summary>
  1143.     ''' Determines whether a registry subkey exists.
  1144.     ''' </summary>
  1145.     ''' <param name="fullKeyPath">The registry key full path.</param>
  1146.     ''' <param name="valueName">The value name.</param>
  1147.     ''' <returns><c>true</c> if subkey exist, <c>false</c> otherwise.</returns>
  1148.     Public Shared Function ExistValue(ByVal fullKeyPath As String,
  1149.                                       ByVal valueName As String) As Boolean
  1150.  
  1151.         Return ExistValue(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath), valueName)
  1152.  
  1153.     End Function
  1154.  
  1155.     ''' <summary>
  1156.     ''' Determines whether a registry value is empty.
  1157.     ''' </summary>
  1158.     ''' <param name="rootKeyName">The rootkey name.</param>
  1159.     ''' <param name="subKeyPath">The subkey path.</param>
  1160.     ''' <param name="valueName">The value name.</param>
  1161.     ''' <returns><c>true</c> if value is empty, <c>false</c> otherwise.</returns>
  1162.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath or valueName</exception>
  1163.     Public Shared Function ValueIsEmpty(ByVal rootKeyName As String,
  1164.                                         ByVal subKeyPath As String,
  1165.                                         ByVal valueName As String) As Boolean
  1166.  
  1167.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  1168.             Throw New ArgumentNullException("rootKeyName")
  1169.  
  1170.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  1171.             Throw New ArgumentNullException("subKeyPath")
  1172.  
  1173.         ElseIf String.IsNullOrEmpty(valueName) OrElse String.IsNullOrWhiteSpace(valueName) Then
  1174.             Throw New ArgumentNullException("valueName")
  1175.  
  1176.         Else
  1177.             Dim reg As RegistryKey = Nothing
  1178.             Try
  1179.                 reg = GetRootKey(rootKeyName)
  1180.                 Using reg
  1181.                     Return String.IsNullOrEmpty(reg.OpenSubKey(GetSubKeyPath(subKeyPath), writable:=False).
  1182.                                                     GetValue(valueName, defaultValue:=Nothing).ToString)
  1183.                 End Using
  1184.  
  1185.             Catch ex As Exception
  1186.                 Throw
  1187.  
  1188.             Finally
  1189.                 If reg IsNot Nothing Then
  1190.                     reg.Close()
  1191.                 End If
  1192.  
  1193.             End Try
  1194.         End If
  1195.  
  1196.     End Function
  1197.  
  1198.     ''' <summary>
  1199.     ''' Determines whether a registry value is empty.
  1200.     ''' </summary>
  1201.     ''' <param name="fullKeyPath">The registry key full path.</param>
  1202.     ''' <param name="valueName">The value name.</param>
  1203.     ''' <returns><c>true</c> if value is empty, <c>false</c> otherwise.</returns>
  1204.     Public Shared Function ValueIsEmpty(ByVal fullKeyPath As String,
  1205.                                         ByVal valueName As String) As Boolean
  1206.  
  1207.         Return ValueIsEmpty(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath), valueName)
  1208.  
  1209.     End Function
  1210.  
  1211. #End Region
  1212.  
  1213. #Region " Import / Export "
  1214.  
  1215.     ''' <summary>
  1216.     ''' Imports a registry file into the current registry Hive.
  1217.     ''' </summary>
  1218.     ''' <param name="regFilePath">The registry filepath.</param>
  1219.     ''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
  1220.     ''' <exception cref="System.ArgumentNullException">regFilePath</exception>
  1221.     Public Shared Function ImportRegFile(ByVal regFilePath As String) As Boolean
  1222.  
  1223.         If String.IsNullOrEmpty(regFilePath) OrElse String.IsNullOrWhiteSpace(regFilePath) Then
  1224.             Throw New ArgumentNullException("regFilePath")
  1225.  
  1226.         Else
  1227.             Try
  1228.                 Using proc As New Process With {
  1229.                     .StartInfo = New ProcessStartInfo() With {
  1230.                           .FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Reg.exe"),
  1231.                           .Arguments = String.Format("Import ""{0}""", regFilePath),
  1232.                           .CreateNoWindow = True,
  1233.                           .WindowStyle = ProcessWindowStyle.Hidden,
  1234.                           .UseShellExecute = False
  1235.                         }
  1236.                     }
  1237.  
  1238.                     With proc
  1239.                         .Start()
  1240.                         .WaitForExit()
  1241.                     End With
  1242.  
  1243.                     Return Not CBool(proc.ExitCode)
  1244.  
  1245.                 End Using
  1246.  
  1247.             Catch ex As Exception
  1248.                 Throw
  1249.  
  1250.             End Try
  1251.  
  1252.         End If
  1253.  
  1254.     End Function
  1255.  
  1256.     ''' <summary>
  1257.     ''' Exports a key to a registry file.
  1258.     ''' </summary>
  1259.     ''' <param name="rootKeyName">The rootkey name.</param>
  1260.     ''' <param name="subKeyPath">The subkey path.</param>
  1261.     ''' <param name="OutputFile">Indicates the output file.</param>
  1262.     ''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
  1263.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath or outputFile</exception>
  1264.     Public Shared Function ExportKey(ByVal rootKeyName As String,
  1265.                                      ByVal subKeyPath As String,
  1266.                                      ByVal outputFile As String) As Boolean
  1267.  
  1268.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  1269.             Throw New ArgumentNullException("rootKeyName")
  1270.  
  1271.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  1272.             Throw New ArgumentNullException("subKeyPath")
  1273.  
  1274.         ElseIf String.IsNullOrEmpty(outputFile) OrElse String.IsNullOrWhiteSpace(outputFile) Then
  1275.             Throw New ArgumentNullException("outputFile")
  1276.  
  1277.         Else
  1278.             Dim reg As RegistryKey = Nothing
  1279.             Try
  1280.                 reg = GetRootKey(rootKeyName)
  1281.                 Using reg
  1282.                     Using proc As New Process With {
  1283.                             .StartInfo = New ProcessStartInfo() With {
  1284.                                   .FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Reg.exe"),
  1285.                                   .Arguments = String.Format("Export ""{0}\{1}"" ""{2}"" /y",
  1286.                                                              GetRootKeyName(rootKeyName),
  1287.                                                              GetSubKeyPath(subKeyPath),
  1288.                                                              outputFile),
  1289.                                   .CreateNoWindow = True,
  1290.                                   .WindowStyle = ProcessWindowStyle.Hidden,
  1291.                                   .UseShellExecute = False
  1292.                                 }
  1293.                             }
  1294.  
  1295.                         With proc
  1296.                             .Start()
  1297.                             .WaitForExit()
  1298.                         End With
  1299.  
  1300.                         Return Not CBool(proc.ExitCode)
  1301.  
  1302.                     End Using
  1303.  
  1304.                 End Using
  1305.  
  1306.             Catch ex As Exception
  1307.                 Throw
  1308.  
  1309.             Finally
  1310.                 If reg IsNot Nothing Then
  1311.                     reg.Close()
  1312.                 End If
  1313.  
  1314.             End Try
  1315.  
  1316.         End If
  1317.  
  1318.     End Function
  1319.  
  1320.     ''' <summary>
  1321.     ''' Exports a key to a registry file.
  1322.     ''' </summary>
  1323.     ''' <param name="fullKeyPath">The registry key full path.</param>
  1324.     ''' <param name="OutputFile">Indicates the output file.</param>
  1325.     ''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
  1326.     Public Shared Function ExportKey(ByVal fullKeyPath As String,
  1327.                                      ByVal outputFile As String) As Boolean
  1328.  
  1329.         Return ExportKey(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath), outputFile)
  1330.  
  1331.     End Function
  1332.  
  1333. #End Region
  1334.  
  1335. #Region " Navigation "
  1336.  
  1337.     ''' <summary>
  1338.     ''' Runs Regedit.exe process to jump at the specified key.
  1339.     ''' </summary>
  1340.     ''' <param name="rootKeyName">The rootkey name.</param>
  1341.     ''' <param name="subKeyPath">The subkey path.</param>
  1342.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath</exception>
  1343.     Public Shared Sub JumpToKey(ByVal rootKeyName As String,
  1344.                                 ByVal subKeyPath As String)
  1345.  
  1346.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  1347.             Throw New ArgumentNullException("rootKeyName")
  1348.  
  1349.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  1350.             Throw New ArgumentNullException("subKeyPath")
  1351.  
  1352.         Else
  1353.             Try
  1354.                 CreateValue(rootKeyName:="HKCU",
  1355.                                     subKeyPath:="Software\Microsoft\Windows\CurrentVersion\Applets\Regedit",
  1356.                                     valueName:="LastKey",
  1357.                                     valueData:=String.Format("{0}\{1}", GetRootKeyName(rootKeyName), GetSubKeyPath(subKeyPath)),
  1358.                                     valueType:=RegistryValueKind.String)
  1359.  
  1360.                 Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Regedit.exe"))
  1361.  
  1362.             Catch ex As Exception
  1363.                 Throw
  1364.  
  1365.             End Try
  1366.  
  1367.         End If
  1368.  
  1369.     End Sub
  1370.  
  1371.     ''' <summary>
  1372.     ''' Runs Regedit.exe process to jump at the specified key.
  1373.     ''' </summary>
  1374.     ''' <param name="fullKeyPath">The registry key full path.</param>
  1375.     Public Shared Sub JumpToKey(ByVal fullKeyPath As String)
  1376.  
  1377.         JumpToKey(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath))
  1378.  
  1379.     End Sub
  1380.  
  1381. #End Region
  1382.  
  1383. #Region " Copying "
  1384.  
  1385.     ''' <summary>
  1386.     ''' Copies a registry value (with its data) to another subkey.
  1387.     ''' </summary>
  1388.     ''' <param name="sourceFullKeyPath">The source registry key full path.</param>
  1389.     ''' <param name="sourceValueName">The source registry value name.</param>
  1390.     ''' <param name="targetFullKeyPath">The target registry key full path.</param>
  1391.     ''' <param name="targetValueName">The target registry value name.</param>
  1392.     Public Shared Sub CopyValue(ByVal sourceFullKeyPath As String,
  1393.                                 ByVal sourceValueName As String,
  1394.                                 ByVal targetFullKeyPath As String,
  1395.                                 ByVal targetValueName As String)
  1396.  
  1397.         CreateValue(fullKeyPath:=targetFullKeyPath,
  1398.                     valueName:=targetValueName,
  1399.                     valueData:=GetValueData(fullKeyPath:=sourceFullKeyPath, valueName:=sourceValueName),
  1400.                     valueType:=RegistryValueKind.Unknown)
  1401.  
  1402.     End Sub
  1403.  
  1404.     ''' <summary>
  1405.     ''' Copies a registry value (with its data) to another subkey.
  1406.     ''' </summary>
  1407.     ''' <param name="sourceRootKeyName">The source registry rootkey name.</param>
  1408.     ''' <param name="sourceSubKeyPath">The source registry subkey path.</param>
  1409.     ''' <param name="sourceValueName">The source registry value name.</param>
  1410.     ''' <param name="targetRootKeyName">The target registry rootkey name.</param>
  1411.     ''' <param name="targetSubKeyPath">The target registry subkey path.</param>
  1412.     ''' <param name="targetValueName">The target registry value name.</param>
  1413.     Public Shared Sub CopyValue(ByVal sourceRootKeyName As String,
  1414.                                 ByVal sourceSubKeyPath As String,
  1415.                                 ByVal sourceValueName As String,
  1416.                                 ByVal targetRootKeyName As String,
  1417.                                 ByVal targetSubKeyPath As String,
  1418.                                 ByVal targetValueName As String)
  1419.  
  1420.         CreateValue(rootKeyName:=targetRootKeyName,
  1421.                     subKeyPath:=targetSubKeyPath,
  1422.                     valueName:=targetValueName,
  1423.                     valueData:=GetValueData(rootKeyName:=sourceRootKeyName, subKeyPath:=sourceSubKeyPath, valueName:=sourceValueName),
  1424.                     valueType:=RegistryValueKind.Unknown)
  1425.  
  1426.     End Sub
  1427.  
  1428.     ''' <summary>
  1429.     ''' Moves a registry value (with its data) to another subkey.
  1430.     ''' </summary>
  1431.     ''' <param name="sourceFullKeyPath">The source registry key full path.</param>
  1432.     ''' <param name="sourceValueName">The source registry value name.</param>
  1433.     ''' <param name="targetFullKeyPath">The target registry key full path.</param>
  1434.     ''' <param name="targetValueName">The target registry value name.</param>
  1435.     Public Shared Sub MoveValue(ByVal sourceFullKeyPath As String,
  1436.                                 ByVal sourceValueName As String,
  1437.                                 ByVal targetFullKeyPath As String,
  1438.                                 ByVal targetValueName As String)
  1439.  
  1440.         CreateValue(fullKeyPath:=targetFullKeyPath,
  1441.                     valueName:=targetValueName,
  1442.                     valueData:=GetValueData(fullKeyPath:=sourceFullKeyPath, valueName:=sourceValueName),
  1443.                     valueType:=RegistryValueKind.Unknown)
  1444.  
  1445.         DeleteValue(fullKeyPath:=sourceFullKeyPath, valueName:=sourceValueName, throwOnMissingValue:=True)
  1446.  
  1447.     End Sub
  1448.  
  1449.     ''' <summary>
  1450.     ''' Moves a registry value (with its data) to another subkey.
  1451.     ''' </summary>
  1452.     ''' <param name="sourceRootKeyName">The source registry rootkey name.</param>
  1453.     ''' <param name="sourceSubKeyPath">The source registry subkey path.</param>
  1454.     ''' <param name="sourceValueName">The source registry value name.</param>
  1455.     ''' <param name="targetRootKeyName">The target registry rootkey name.</param>
  1456.     ''' <param name="targetSubKeyPath">The target registry subkey path.</param>
  1457.     ''' <param name="targetValueName">The target registry value name.</param>
  1458.     Public Shared Sub MoveValue(ByVal sourceRootKeyName As String,
  1459.                                 ByVal sourceSubKeyPath As String,
  1460.                                 ByVal sourceValueName As String,
  1461.                                 ByVal targetRootKeyName As String,
  1462.                                 ByVal targetSubKeyPath As String,
  1463.                                 ByVal targetValueName As String)
  1464.  
  1465.         CreateValue(rootKeyName:=targetRootKeyName,
  1466.                     subKeyPath:=targetSubKeyPath,
  1467.                     valueName:=targetValueName,
  1468.                     valueData:=GetValueData(rootKeyName:=sourceRootKeyName, subKeyPath:=sourceSubKeyPath, valueName:=sourceValueName),
  1469.                     valueType:=RegistryValueKind.Unknown)
  1470.  
  1471.         DeleteValue(rootKeyName:=sourceRootKeyName, subKeyPath:=sourceSubKeyPath, valueName:=sourceValueName, throwOnMissingValue:=True)
  1472.  
  1473.     End Sub
  1474.  
  1475.     ''' <summary>
  1476.     ''' Copies a registry key tree to another registry path.
  1477.     ''' </summary>
  1478.     ''' <param name="sourceRootKeyName">The source registry rootkey name.</param>
  1479.     ''' <param name="sourceSubKeyPath">The source registry subkey path.</param>
  1480.     ''' <param name="targetRootKeyName">The target registry rootkey name.</param>
  1481.     ''' <param name="targetSubKeyPath">The target registry subkey path.</param>
  1482.     ''' <exception cref="System.ArgumentNullException">
  1483.     ''' sourceRootKeyName or sourceSubKeyPath or targetRootKeyName or targetSubKeyPath
  1484.     ''' </exception>
  1485.     Public Shared Sub CopyKeyTree(ByVal sourceRootKeyName As String,
  1486.                                   ByVal sourceSubKeyPath As String,
  1487.                                   ByVal targetRootKeyName As String,
  1488.                                   ByVal targetSubKeyPath As String)
  1489.  
  1490.         If String.IsNullOrEmpty(sourceRootKeyName) OrElse String.IsNullOrWhiteSpace(sourceRootKeyName) Then
  1491.             Throw New ArgumentNullException("sourceRootKeyName")
  1492.  
  1493.         ElseIf String.IsNullOrEmpty(sourceSubKeyPath) OrElse String.IsNullOrWhiteSpace(sourceSubKeyPath) Then
  1494.             Throw New ArgumentNullException("sourceSubKeyPath")
  1495.  
  1496.         ElseIf String.IsNullOrEmpty(targetRootKeyName) OrElse String.IsNullOrWhiteSpace(targetRootKeyName) Then
  1497.             Throw New ArgumentNullException("targetRootKeyName")
  1498.  
  1499.         ElseIf String.IsNullOrEmpty(targetSubKeyPath) OrElse String.IsNullOrWhiteSpace(targetSubKeyPath) Then
  1500.             Throw New ArgumentNullException("targetSubKeyPath")
  1501.  
  1502.         Else
  1503.  
  1504.             Dim sourceRegistryKey As RegistryKey = Nothing
  1505.             Dim targetRegistryKey As RegistryKey = Nothing
  1506.  
  1507.             Try
  1508.                 sourceRegistryKey = GetRootKey(sourceRootKeyName).OpenSubKey(GetSubKeyPath(sourceSubKeyPath), writable:=False)
  1509.                 Using sourceRegistryKey
  1510.  
  1511.                     CreateSubKey(rootKeyName:=GetRootKeyName(targetRootKeyName), subKeyPath:=GetSubKeyPath(targetSubKeyPath))
  1512.  
  1513.                     targetRegistryKey = GetRootKey(targetRootKeyName).OpenSubKey(GetSubKeyPath(targetSubKeyPath), writable:=True)
  1514.                     Using targetRegistryKey
  1515.                         CopySubKeys(sourceRegistryKey, targetRegistryKey)
  1516.                     End Using
  1517.  
  1518.                 End Using
  1519.  
  1520.             Catch ex As Exception
  1521.                 Throw
  1522.  
  1523.             Finally
  1524.                 If sourceRegistryKey IsNot Nothing Then
  1525.                     sourceRegistryKey.Close()
  1526.                 End If
  1527.                 If targetRegistryKey IsNot Nothing Then
  1528.                     targetRegistryKey.Close()
  1529.                 End If
  1530.  
  1531.             End Try
  1532.  
  1533.         End If
  1534.  
  1535.     End Sub
  1536.  
  1537.     ''' <summary>
  1538.     ''' Copies a registry key tree to another registry path.
  1539.     ''' </summary>
  1540.     ''' <param name="sourceFullKeyPath">The source registry key full path.</param>
  1541.     ''' <param name="targetFullKeyPath">The target registry key full path.</param>
  1542.     Public Shared Sub CopyKeyTree(ByVal sourceFullKeyPath As String,
  1543.                                   ByVal targetFullKeyPath As String)
  1544.  
  1545.         CopyKeyTree(sourceRootKeyName:=GetRootKeyName(sourceFullKeyPath),
  1546.                     sourceSubKeyPath:=GetSubKeyPath(sourceFullKeyPath),
  1547.                     targetRootKeyName:=GetRootKeyName(targetFullKeyPath),
  1548.                     targetSubKeyPath:=GetSubKeyPath(targetFullKeyPath))
  1549.  
  1550.     End Sub
  1551.  
  1552.     ''' <summary>
  1553.     ''' Copies the sub-keys of the specified registry key.
  1554.     ''' </summary>
  1555.     ''' <param name="sourceRegistryKey">Indicates the old key.</param>
  1556.     ''' <param name="targetRegistryKey">Indicates the new key.</param>
  1557.     Private Shared Sub CopySubKeys(ByVal sourceRegistryKey As RegistryKey,
  1558.                                    ByVal targetRegistryKey As RegistryKey)
  1559.  
  1560.         ' Copy Values.
  1561.         For Each valueName As String In sourceRegistryKey.GetValueNames()
  1562.             targetRegistryKey.SetValue(valueName, sourceRegistryKey.GetValue(valueName))
  1563.         Next valueName
  1564.  
  1565.         ' Copy Subkeys.
  1566.         For Each subKeyName As String In sourceRegistryKey.GetSubKeyNames
  1567.  
  1568.             CreateSubKey(fullKeyPath:=String.Format("{0}\{1}", targetRegistryKey.Name, subKeyName))
  1569.             CopySubKeys(sourceRegistryKey.OpenSubKey(subKeyName, writable:=False),
  1570.                         targetRegistryKey.OpenSubKey(subKeyName, writable:=True))
  1571.  
  1572.         Next subKeyName
  1573.  
  1574.     End Sub
  1575.  
  1576.     ''' <summary>
  1577.     ''' Copies the sub-keys of the specified registry key.
  1578.     ''' </summary>
  1579.     ''' <param name="sourceRootKeyName">The source registry rootkey name.</param>
  1580.     ''' <param name="sourceSubKeyPath">The source registry subkey path.</param>
  1581.     ''' <param name="targetRootKeyName">The target registry rootkey name.</param>
  1582.     ''' <param name="targetSubKeyPath">The target registry subkey path.</param>
  1583.     Public Shared Sub CopySubKeys(ByVal sourceRootKeyName As String,
  1584.                                   ByVal sourceSubKeyPath As String,
  1585.                                   ByVal targetRootKeyName As String,
  1586.                                   ByVal targetSubKeyPath As String)
  1587.  
  1588.         Dim sourceRegistryKey As RegistryKey = Nothing
  1589.         Dim targetRegistryKey As RegistryKey = Nothing
  1590.  
  1591.         Try
  1592.             sourceRegistryKey = GetRootKey(sourceRootKeyName).OpenSubKey(GetSubKeyPath(sourceSubKeyPath), writable:=False)
  1593.             targetRegistryKey = GetRootKey(targetRootKeyName).OpenSubKey(GetSubKeyPath(targetSubKeyPath), writable:=True)
  1594.  
  1595.             CopySubKeys(sourceRegistryKey, targetRegistryKey)
  1596.  
  1597.         Catch ex As Exception
  1598.             Throw
  1599.  
  1600.         Finally
  1601.             If sourceRegistryKey IsNot Nothing Then
  1602.                 sourceRegistryKey.Close()
  1603.             End If
  1604.             If targetRegistryKey IsNot Nothing Then
  1605.                 targetRegistryKey.Close()
  1606.             End If
  1607.  
  1608.         End Try
  1609.  
  1610.     End Sub
  1611.  
  1612.     ''' <summary>
  1613.     ''' Copies the sub-keys of the specified registry key.
  1614.     ''' </summary>
  1615.     ''' <param name="sourceFullKeyPath">The source registry key full path.</param>
  1616.     ''' <param name="targetFullKeyPath">The target registry key full path.</param>
  1617.     Public Shared Sub CopySubKeys(ByVal sourceFullKeyPath As String,
  1618.                                   ByVal targetFullKeyPath As String)
  1619.  
  1620.         CopySubKeys(sourceRootKeyName:=GetRootKeyName(sourceFullKeyPath),
  1621.                     sourceSubKeyPath:=GetSubKeyPath(sourceFullKeyPath),
  1622.                     targetRootKeyName:=GetRootKeyName(targetFullKeyPath),
  1623.                     targetSubKeyPath:=GetSubKeyPath(targetFullKeyPath))
  1624.  
  1625.     End Sub
  1626.  
  1627.     ''' <summary>
  1628.     ''' Moves a registry key tree to another registry path.
  1629.     ''' </summary>
  1630.     ''' <param name="sourceRootKeyName">The source registry rootkey name.</param>
  1631.     ''' <param name="sourceSubKeyPath">The source registry subkey path.</param>
  1632.     ''' <param name="targetRootKeyName">The target registry rootkey name.</param>
  1633.     ''' <param name="targetSubKeyPath">The target registry subkey path.</param>
  1634.     ''' <exception cref="System.ArgumentNullException">
  1635.     ''' sourceRootKeyName or sourceSubKeyPath or targetRootKeyName or targetSubKeyPath
  1636.     ''' </exception>
  1637.     Public Shared Sub MoveKeyTree(ByVal sourceRootKeyName As String,
  1638.                                   ByVal sourceSubKeyPath As String,
  1639.                                   ByVal targetRootKeyName As String,
  1640.                                   ByVal targetSubKeyPath As String)
  1641.  
  1642.         If String.IsNullOrEmpty(sourceRootKeyName) OrElse String.IsNullOrWhiteSpace(sourceRootKeyName) Then
  1643.             Throw New ArgumentNullException("sourceRootKeyName")
  1644.  
  1645.         ElseIf String.IsNullOrEmpty(sourceSubKeyPath) OrElse String.IsNullOrWhiteSpace(sourceSubKeyPath) Then
  1646.             Throw New ArgumentNullException("sourceSubKeyPath")
  1647.  
  1648.         ElseIf String.IsNullOrEmpty(targetRootKeyName) OrElse String.IsNullOrWhiteSpace(targetRootKeyName) Then
  1649.             Throw New ArgumentNullException("targetRootKeyName")
  1650.  
  1651.         ElseIf String.IsNullOrEmpty(targetSubKeyPath) OrElse String.IsNullOrWhiteSpace(targetSubKeyPath) Then
  1652.             Throw New ArgumentNullException("targetSubKeyPath")
  1653.  
  1654.         Else
  1655.  
  1656.             Dim sourceRegistryKey As RegistryKey = Nothing
  1657.             Dim targetRegistryKey As RegistryKey = Nothing
  1658.  
  1659.             Try
  1660.                 sourceRegistryKey = GetRootKey(sourceRootKeyName).OpenSubKey(GetSubKeyPath(sourceSubKeyPath), writable:=False)
  1661.                 Using sourceRegistryKey
  1662.  
  1663.                     CreateSubKey(rootKeyName:=GetRootKeyName(sourceRootKeyName), subKeyPath:=GetSubKeyPath(sourceSubKeyPath))
  1664.  
  1665.                     targetRegistryKey = GetRootKey(targetRootKeyName).OpenSubKey(GetSubKeyPath(targetSubKeyPath), writable:=True)
  1666.                     Using targetRegistryKey
  1667.                         CopySubKeys(sourceRegistryKey, targetRegistryKey)
  1668.                     End Using
  1669.  
  1670.                 End Using
  1671.  
  1672.                 DeleteSubKey(GetRootKeyName(sourceRootKeyName), GetSubKeyPath(sourceSubKeyPath))
  1673.  
  1674.             Catch ex As Exception
  1675.                 Throw
  1676.  
  1677.             Finally
  1678.                 If sourceRegistryKey IsNot Nothing Then
  1679.                     sourceRegistryKey.Close()
  1680.                 End If
  1681.                 If targetRegistryKey IsNot Nothing Then
  1682.                     targetRegistryKey.Close()
  1683.                 End If
  1684.  
  1685.             End Try
  1686.  
  1687.         End If
  1688.  
  1689.     End Sub
  1690.  
  1691.     ''' <summary>
  1692.     ''' Moves a registry key tree to another registry path.
  1693.     ''' </summary>
  1694.     ''' <param name="sourceFullKeyPath">The source registry key full path.</param>
  1695.     ''' <param name="targetFullKeyPath">The target registry key full path.</param>
  1696.     Public Shared Sub MoveKeyTree(ByVal sourceFullKeyPath As String,
  1697.                                   ByVal targetFullKeyPath As String)
  1698.  
  1699.         MoveKeyTree(sourceRootKeyName:=GetRootKeyName(sourceFullKeyPath),
  1700.                     sourceSubKeyPath:=GetSubKeyPath(sourceFullKeyPath),
  1701.                     targetRootKeyName:=GetRootKeyName(targetFullKeyPath),
  1702.                     targetSubKeyPath:=GetSubKeyPath(targetFullKeyPath))
  1703.  
  1704.     End Sub
  1705.  
  1706.     ''' <summary>
  1707.     ''' Moves the sub-keys of the specified registry key.
  1708.     ''' </summary>
  1709.     ''' <param name="sourceRootKeyName">The source registry rootkey name.</param>
  1710.     ''' <param name="sourceSubKeyPath">The source registry subkey path.</param>
  1711.     ''' <param name="targetRootKeyName">The target registry rootkey name.</param>
  1712.     ''' <param name="targetSubKeyPath">The target registry subkey path.</param>
  1713.     Public Shared Sub MoveSubKeys(ByVal sourceRootKeyName As String,
  1714.                                   ByVal sourceSubKeyPath As String,
  1715.                                   ByVal targetRootKeyName As String,
  1716.                                   ByVal targetSubKeyPath As String)
  1717.  
  1718.         Dim sourceRegistryKey As RegistryKey = Nothing
  1719.         Dim targetRegistryKey As RegistryKey = Nothing
  1720.  
  1721.         Try
  1722.             CreateSubKey(GetRootKeyName(targetRootKeyName), GetSubKeyPath(targetSubKeyPath))
  1723.  
  1724.             sourceRegistryKey = GetRootKey(sourceRootKeyName).OpenSubKey(GetSubKeyPath(sourceSubKeyPath), writable:=False)
  1725.             targetRegistryKey = GetRootKey(targetRootKeyName).OpenSubKey(GetSubKeyPath(targetSubKeyPath), writable:=True)
  1726.  
  1727.             CopySubKeys(sourceRegistryKey, targetRegistryKey)
  1728.             DeleteSubKey(GetRootKeyName(sourceRootKeyName), GetSubKeyPath(sourceSubKeyPath))
  1729.  
  1730.         Catch ex As Exception
  1731.             Throw
  1732.  
  1733.         Finally
  1734.             If sourceRegistryKey IsNot Nothing Then
  1735.                 sourceRegistryKey.Close()
  1736.             End If
  1737.             If targetRegistryKey IsNot Nothing Then
  1738.                 targetRegistryKey.Close()
  1739.             End If
  1740.  
  1741.         End Try
  1742.  
  1743.     End Sub
  1744.  
  1745.     ''' <summary>
  1746.     ''' Moves the sub-keys of the specified registry key.
  1747.     ''' </summary>
  1748.     ''' <param name="sourceFullKeyPath">The source registry key full path.</param>
  1749.     ''' <param name="targetFullKeyPath">The target registry key full path.</param>
  1750.     Public Shared Sub MoveSubKeys(ByVal sourceFullKeyPath As String,
  1751.                                   ByVal targetFullKeyPath As String)
  1752.  
  1753.         MoveSubKeys(sourceRootKeyName:=GetRootKeyName(sourceFullKeyPath),
  1754.                     sourceSubKeyPath:=GetSubKeyPath(sourceFullKeyPath),
  1755.                     targetRootKeyName:=GetRootKeyName(targetFullKeyPath),
  1756.                     targetSubKeyPath:=GetSubKeyPath(targetFullKeyPath))
  1757.  
  1758.     End Sub
  1759.  
  1760. #End Region
  1761.  
  1762. #Region " Registry Path Formatting "
  1763.  
  1764.     ''' <summary>
  1765.     ''' Gets the root <see cref="RegistryKey"/> of a registry path.
  1766.     ''' </summary>
  1767.     ''' <returns>The root <see cref="RegistryKey"/> of a registry path.</returns>
  1768.     Public Shared Function GetRootKey(ByVal registryPath As String) As RegistryKey
  1769.  
  1770.         Select Case registryPath.ToUpper.Split("\"c).First
  1771.  
  1772.             Case "HKCR", "HKEY_CLASSES_ROOT"
  1773.                 Return Registry.ClassesRoot
  1774.  
  1775.             Case "HKCC", "HKEY_CURRENT_CONFIG"
  1776.                 Return Registry.CurrentConfig
  1777.  
  1778.             Case "HKCU", "HKEY_CURRENT_USER"
  1779.                 Return Registry.CurrentUser
  1780.  
  1781.             Case "HKLM", "HKEY_LOCAL_MACHINE"
  1782.                 Return Registry.LocalMachine
  1783.  
  1784.             Case "HKEY_PERFORMANCE_DATA"
  1785.                 Return Registry.PerformanceData
  1786.  
  1787.             Case Else
  1788.                 Return Nothing
  1789.  
  1790.         End Select
  1791.  
  1792.     End Function
  1793.  
  1794.     ''' <summary>
  1795.     ''' Gets the root key name of a registry path.
  1796.     ''' </summary>
  1797.     ''' <returns>The root key name of a registry path.</returns>
  1798.     Public Shared Function GetRootKeyName(ByVal registryPath As String) As String
  1799.  
  1800.         Select Case registryPath.ToUpper.Split("\"c).FirstOrDefault
  1801.  
  1802.             Case "HKCR", "HKEY_CLASSES_ROOT"
  1803.                 Return "HKEY_CLASSES_ROOT"
  1804.  
  1805.             Case "HKCC", "HKEY_CURRENT_CONFIG"
  1806.                 Return "HKEY_CURRENT_CONFIG"
  1807.  
  1808.             Case "HKCU", "HKEY_CURRENT_USER"
  1809.                 Return "HKEY_CURRENT_USER"
  1810.  
  1811.             Case "HKLM", "HKEY_LOCAL_MACHINE"
  1812.                 Return "HKEY_LOCAL_MACHINE"
  1813.  
  1814.             Case "HKEY_PERFORMANCE_DATA"
  1815.                 Return "HKEY_PERFORMANCE_DATA"
  1816.  
  1817.             Case Else
  1818.                 Return String.Empty
  1819.  
  1820.         End Select
  1821.  
  1822.     End Function
  1823.  
  1824.     ''' <summary>
  1825.     ''' Gets the subkey path of a registry path.
  1826.     ''' </summary>
  1827.     ''' <returns>The root <see cref="RegistryKey"/> of a key-path.</returns>
  1828.     Public Shared Function GetSubKeyPath(ByVal registryPath As String) As String
  1829.  
  1830.         Select Case String.IsNullOrEmpty(GetRootKeyName(registryPath))
  1831.  
  1832.             Case True
  1833.                 Return registryPath.TrimStart("\"c).TrimEnd("\"c)
  1834.  
  1835.             Case Else
  1836.                 Return registryPath.Substring(registryPath.IndexOf("\"c)).TrimStart("\"c).TrimEnd("\"c)
  1837.  
  1838.         End Select
  1839.  
  1840.     End Function
  1841.  
  1842. #End Region
  1843.  
  1844. #Region " Find "
  1845.  
  1846.     ''' <summary>
  1847.     ''' Finds on a registry path all the subkey names that matches the specified criteria.
  1848.     ''' </summary>
  1849.     ''' <param name="rootKeyName">The rootkey name.</param>
  1850.     ''' <param name="subKeyPath">The subkey path.</param>
  1851.     ''' <param name="subKeyName">The subkey name to find.</param>
  1852.     ''' <param name="matchFullSubKeyName">If set to <c>true</c>, matches all the subkey name, otherwise matches a part of the name.</param>
  1853.     ''' <param name="ignoreCase">If set to <c>true</c>, performs a non-sensitive stringcase comparison.</param>
  1854.     ''' <param name="searchOption">The search mode.</param>
  1855.     ''' <returns>IEnumerable(Of RegInfo).</returns>
  1856.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath or subKeyName</exception>
  1857.     Public Shared Function FindSubKey(ByVal rootKeyName As String,
  1858.                                       ByVal subKeyPath As String,
  1859.                                       ByVal subKeyName As String,
  1860.                                       ByVal matchFullSubKeyName As Boolean,
  1861.                                       ByVal ignoreCase As Boolean,
  1862.                                       ByVal searchOption As SearchOption) As IEnumerable(Of RegInfo)
  1863.  
  1864.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  1865.             Throw New ArgumentNullException("rootKeyName")
  1866.  
  1867.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  1868.             Throw New ArgumentNullException("subKeyPath")
  1869.  
  1870.         ElseIf String.IsNullOrEmpty(subKeyName) OrElse String.IsNullOrWhiteSpace(subKeyName) Then
  1871.             Throw New ArgumentNullException("subKeyName")
  1872.  
  1873.         End If
  1874.  
  1875.         Dim reg As RegistryKey = Nothing
  1876.         Try
  1877.             reg = GetRootKey(rootKeyName).OpenSubKey(GetSubKeyPath(subKeyPath), writable:=False)
  1878.             Using reg
  1879.  
  1880.                 If matchFullSubKeyName Then
  1881.  
  1882.                     If ignoreCase Then
  1883.                         Return (From registryPath As String In CollectSubKeys(reg, searchOption)
  1884.                                 Where GetSubKeyPath(registryPath).
  1885.                                       Remove(0, subKeyPath.Length).
  1886.                                       ToLower.
  1887.                                       Split("\"c).
  1888.                                       Contains(subKeyName.ToLower)
  1889.                                 Select New RegInfo With
  1890.                                        {
  1891.                                            .RootKeyName = GetRootKeyName(registryPath),
  1892.                                            .SubKeyPath = GetSubKeyPath(registryPath)
  1893.                                        }).ToArray
  1894.  
  1895.                     Else
  1896.                         Return (From registryPath As String In CollectSubKeys(reg, searchOption)
  1897.                                 Where GetSubKeyPath(registryPath).
  1898.                                       Remove(0, subKeyPath.Length).
  1899.                                       Split("\"c).
  1900.                                       Contains(subKeyName)
  1901.                                 Select New RegInfo With
  1902.                                        {
  1903.                                            .RootKeyName = GetRootKeyName(registryPath),
  1904.                                            .SubKeyPath = GetSubKeyPath(registryPath)
  1905.                                        }).ToArray
  1906.  
  1907.                     End If ' ignoreCase
  1908.  
  1909.                 Else ' not matchFullSubKeyName
  1910.                     If ignoreCase Then
  1911.                         Return (From registryPath As String In CollectSubKeys(reg, searchOption)
  1912.                                Where GetSubKeyPath(registryPath).
  1913.                                      Remove(0, subKeyPath.Length).
  1914.                                      ToLower.
  1915.                                      Contains(subKeyName.ToLower)
  1916.                                 Select New RegInfo With
  1917.                                        {
  1918.                                            .RootKeyName = GetRootKeyName(registryPath),
  1919.                                            .SubKeyPath = GetSubKeyPath(registryPath)
  1920.                                        }).ToArray
  1921.  
  1922.                     Else
  1923.                         Return (From registryPath As String In CollectSubKeys(reg, searchOption)
  1924.                                Where GetSubKeyPath(registryPath).
  1925.                                      Remove(0, subKeyPath.Length).
  1926.                                      Contains(subKeyName)
  1927.                                 Select New RegInfo With
  1928.                                        {
  1929.                                            .RootKeyName = GetRootKeyName(registryPath),
  1930.                                            .SubKeyPath = GetSubKeyPath(registryPath)
  1931.                                        }).ToArray
  1932.  
  1933.                     End If ' ignoreCase
  1934.  
  1935.                 End If ' matchFullSubKeyName
  1936.  
  1937.             End Using
  1938.  
  1939.         Catch ex As Exception
  1940.             Throw
  1941.  
  1942.         Finally
  1943.             If reg IsNot Nothing Then
  1944.                 reg.Close()
  1945.             End If
  1946.  
  1947.         End Try
  1948.  
  1949.     End Function
  1950.  
  1951.     ''' <summary>
  1952.     ''' Finds on a registry path all the subkey names that matches the specified criteria.
  1953.     ''' </summary>
  1954.     ''' <param name="fullKeyPath">The registry key full path.</param>
  1955.     ''' <param name="subKeyName">The subkey name to find.</param>
  1956.     ''' <param name="matchFullSubKeyName">If set to <c>true</c>, matches all the subkey name, otherwise matches a part of the name.</param>
  1957.     ''' <param name="ignoreCase">If set to <c>true</c>, performs a non-sensitive stringcase comparison.</param>
  1958.     ''' <param name="searchOption">The search mode.</param>
  1959.     ''' <returns>IEnumerable(Of RegInfo).</returns>
  1960.     Public Shared Function FindSubKey(ByVal fullKeyPath As String,
  1961.                                       ByVal subKeyName As String,
  1962.                                       ByVal matchFullSubKeyName As Boolean,
  1963.                                       ByVal ignoreCase As Boolean,
  1964.                                       ByVal searchOption As SearchOption) As IEnumerable(Of RegInfo)
  1965.  
  1966.         Return FindSubKey(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath), subKeyName, matchFullSubKeyName, ignoreCase, searchOption)
  1967.  
  1968.     End Function
  1969.  
  1970.     ''' <summary>
  1971.     ''' Finds on a registry path all the value names that matches the specified criteria.
  1972.     ''' </summary>
  1973.     ''' <param name="rootKeyName">The rootkey name.</param>
  1974.     ''' <param name="subKeyPath">The subkey path.</param>
  1975.     ''' <param name="valueName">The value name to find.</param>
  1976.     ''' <param name="matchFullValueName">If set to <c>true</c>, matches all the value name, otherwise matches a part of the name.</param>
  1977.     ''' <param name="ignoreCase">If set to <c>true</c>, performs a non-sensitive stringcase comparison.</param>
  1978.     ''' <param name="searchOption">The search mode.</param>
  1979.     ''' <returns>IEnumerable(Of RegInfo).</returns>
  1980.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath or subKeyName</exception>
  1981.     Public Shared Function FindValue(ByVal rootKeyName As String,
  1982.                                      ByVal subKeyPath As String,
  1983.                                      ByVal valueName As String,
  1984.                                      ByVal matchFullValueName As Boolean,
  1985.                                      ByVal ignoreCase As Boolean,
  1986.                                      ByVal searchOption As SearchOption) As IEnumerable(Of RegInfo)
  1987.  
  1988.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  1989.             Throw New ArgumentNullException("rootKeyName")
  1990.  
  1991.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  1992.             Throw New ArgumentNullException("subKeyPath")
  1993.  
  1994.         ElseIf String.IsNullOrEmpty(valueName) OrElse String.IsNullOrWhiteSpace(valueName) Then
  1995.             Throw New ArgumentNullException("valueName")
  1996.  
  1997.         End If
  1998.  
  1999.         Dim reg As RegistryKey = Nothing
  2000.         Try
  2001.             reg = GetRootKey(rootKeyName).OpenSubKey(GetSubKeyPath(subKeyPath), writable:=False)
  2002.             Using reg
  2003.  
  2004.                 If matchFullValueName Then
  2005.  
  2006.                     If ignoreCase Then
  2007.                         Return (From regInfo As reginfo In CollectValues(reg, searchOption)
  2008.                                 Where regInfo.ValueName.ToLower.Equals(valueName.ToLower)).ToArray
  2009.  
  2010.                     Else
  2011.                         Return (From regInfo As reginfo In CollectValues(reg, searchOption)
  2012.                                 Where regInfo.ValueName.Equals(valueName)).ToArray
  2013.  
  2014.                     End If ' ignoreCase
  2015.  
  2016.                 Else ' not matchFullValueName
  2017.                     If ignoreCase Then
  2018.                         Return (From regInfo As reginfo In CollectValues(reg, searchOption)
  2019.                                 Where regInfo.ValueName.ToLower.Contains(valueName.ToLower)).ToArray
  2020.  
  2021.                     Else
  2022.                         Return (From regInfo As reginfo In CollectValues(reg, searchOption)
  2023.                                 Where regInfo.ValueName.Contains(valueName)).ToArray
  2024.  
  2025.                     End If ' ignoreCase
  2026.  
  2027.                 End If ' matchFullValueName
  2028.  
  2029.             End Using
  2030.  
  2031.         Catch ex As Exception
  2032.             Throw
  2033.  
  2034.         Finally
  2035.             If reg IsNot Nothing Then
  2036.                 reg.Close()
  2037.             End If
  2038.  
  2039.         End Try
  2040.  
  2041.     End Function
  2042.  
  2043.     ''' <summary>
  2044.     ''' Finds on a registry path all the value names that matches the specified criteria.
  2045.     ''' </summary>
  2046.     ''' <param name="fullKeyPath">The registry key full path.</param>
  2047.     ''' <param name="valueName">The value name to find.</param>
  2048.     ''' <param name="matchFullValueName">If set to <c>true</c>, matches all the value name, otherwise matches a part of the name.</param>
  2049.     ''' <param name="ignoreCase">If set to <c>true</c>, performs a non-sensitive stringcase comparison.</param>
  2050.     ''' <param name="searchOption">The search mode.</param>
  2051.     ''' <returns>IEnumerable(Of RegInfo).</returns>
  2052.     Public Shared Function FindValue(ByVal fullKeyPath As String,
  2053.                                      ByVal valueName As String,
  2054.                                      ByVal matchFullValueName As Boolean,
  2055.                                      ByVal ignoreCase As Boolean,
  2056.                                      ByVal searchOption As SearchOption) As IEnumerable(Of RegInfo)
  2057.  
  2058.         Return FindValue(GetRootKeyName(fullKeyPath), GetSubKeyPath(fullKeyPath), valueName, matchFullValueName, ignoreCase, searchOption)
  2059.  
  2060.     End Function
  2061.  
  2062.     ''' <summary>
  2063.     ''' Finds on a registry path all the values that contains data that matches the specified criteria.
  2064.     ''' </summary>
  2065.     ''' <param name="rootKeyName">The rootkey name.</param>
  2066.     ''' <param name="subKeyPath">The subkey path.</param>
  2067.     ''' <param name="valueData">The data to find.</param>
  2068.     ''' <param name="matchFullData">If set to <c>true</c>, matches all the data, otherwise matches a part of the data.</param>
  2069.     ''' <param name="ignoreCase">If set to <c>true</c>, performs a non-sensitive stringcase comparison (for String data).</param>
  2070.     ''' <param name="searchOption">The search mode.</param>
  2071.     ''' <returns>IEnumerable(Of RegInfo).</returns>
  2072.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath or subKeyName</exception>
  2073.     Public Shared Function FindValueData(ByVal rootKeyName As String,
  2074.                                          ByVal subKeyPath As String,
  2075.                                          ByVal valueData As String,
  2076.                                          ByVal matchFullData As Boolean,
  2077.                                          ByVal ignoreCase As Boolean,
  2078.                                          ByVal searchOption As SearchOption) As IEnumerable(Of RegInfo)
  2079.  
  2080.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  2081.             Throw New ArgumentNullException("rootKeyName")
  2082.  
  2083.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  2084.             Throw New ArgumentNullException("subKeyPath")
  2085.  
  2086.         ElseIf String.IsNullOrEmpty(valueData) OrElse String.IsNullOrWhiteSpace(valueData) Then
  2087.             Throw New ArgumentNullException("valueData")
  2088.  
  2089.         End If
  2090.  
  2091.         Dim reg As RegistryKey = Nothing
  2092.         Try
  2093.             reg = GetRootKey(rootKeyName).OpenSubKey(GetSubKeyPath(subKeyPath), writable:=False)
  2094.             Using reg
  2095.  
  2096.                 If matchFullData Then
  2097.  
  2098.                     If ignoreCase Then
  2099.                         Return (From regInfo As reginfo In CollectValueDatas(reg, searchOption).ToArray
  2100.                                 Where Not String.IsNullOrEmpty(regInfo.ValueData.ToString) AndAlso
  2101.                                       regInfo.ValueData.ToString.ToLower.Equals(valueData.ToLower))
  2102.  
  2103.                     Else
  2104.                         Return (From regInfo As reginfo In CollectValueDatas(reg, searchOption).ToArray
  2105.                                 Where Not String.IsNullOrEmpty(regInfo.ValueData.ToString) AndAlso
  2106.                                       regInfo.ValueData.ToString.Equals(valueData))
  2107.  
  2108.                     End If ' ignoreCase
  2109.  
  2110.                 Else ' not matchFullData
  2111.                     If ignoreCase Then
  2112.                         Return (From regInfo As reginfo In CollectValueDatas(reg, searchOption).ToArray
  2113.                                 Where Not String.IsNullOrEmpty(regInfo.ValueData.ToString) AndAlso
  2114.                                       regInfo.ValueData.ToString.ToLower.Contains(valueData.ToLower))
  2115.  
  2116.                     Else
  2117.                         Return (From regInfo As reginfo In CollectValueDatas(reg, searchOption).ToArray
  2118.                                 Where Not String.IsNullOrEmpty(regInfo.ValueData.ToString) AndAlso
  2119.                                       regInfo.ValueData.ToString.Contains(valueData))
  2120.  
  2121.                     End If ' ignoreCase
  2122.  
  2123.                 End If ' matchFullData
  2124.  
  2125.             End Using
  2126.  
  2127.         Catch ex As Exception
  2128.             Throw
  2129.  
  2130.         Finally
  2131.             If reg IsNot Nothing Then
  2132.                 reg.Close()
  2133.             End If
  2134.  
  2135.         End Try
  2136.  
  2137.     End Function
  2138.  
  2139. #End Region
  2140.  
  2141. #Region " Authentication "
  2142.  
  2143.     ''' <summary>
  2144.     ''' Modifies the user permissions of a registry key.
  2145.     ''' </summary>
  2146.     ''' <param name="rootKeyName">The rootkey name.</param>
  2147.     ''' <param name="subKeyPath">The subkey path.</param>
  2148.     ''' <param name="userAccess">The user access.</param>
  2149.     ''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
  2150.     ''' <exception cref="System.ArgumentNullException">rootKeyName or subKeyPath</exception>
  2151.     Public Shared Function SetUserAccessKey(ByVal rootKeyName As String,
  2152.                                             ByVal subKeyPath As String,
  2153.                                             ByVal userAccess() As ReginiUserAccess) As Boolean
  2154.  
  2155.         If String.IsNullOrEmpty(rootKeyName) OrElse String.IsNullOrWhiteSpace(rootKeyName) Then
  2156.             Throw New ArgumentNullException("rootKeyName")
  2157.  
  2158.         ElseIf String.IsNullOrEmpty(subKeyPath) OrElse String.IsNullOrWhiteSpace(subKeyPath) Then
  2159.             Throw New ArgumentNullException("subKeyPath")
  2160.         Else
  2161.  
  2162.             Dim tmpFilePath As String = Path.Combine(Path.GetTempPath(), "Regini.ini")
  2163.  
  2164.             Dim permissionString As String =
  2165.                 String.Format("[{0}]",
  2166.                               String.Join(" "c, userAccess.Cast(Of Integer)))
  2167.  
  2168.             Try
  2169.                 Using textFile As New StreamWriter(path:=tmpFilePath, append:=False, encoding:=Encoding.Default)
  2170.  
  2171.                     textFile.WriteLine(String.Format("""{0}\{1}"" {2}",
  2172.                                                      GetRootKeyName(rootKeyName),
  2173.                                                      GetSubKeyPath(subKeyPath),
  2174.                                                      permissionString))
  2175.  
  2176.                 End Using ' TextFile
  2177.  
  2178.                 Using proc As New Process With {
  2179.                     .StartInfo = New ProcessStartInfo() With {
  2180.                            .FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Regini.exe"),
  2181.                            .Arguments = ControlChars.Quote & tmpFilePath & ControlChars.Quote,
  2182.                            .CreateNoWindow = True,
  2183.                            .WindowStyle = ProcessWindowStyle.Hidden,
  2184.                            .UseShellExecute = False
  2185.                         }
  2186.                     }
  2187.  
  2188.                     With proc
  2189.                         .Start()
  2190.                         .WaitForExit()
  2191.                     End With
  2192.  
  2193.                     Return Not CBool(proc.ExitCode)
  2194.  
  2195.                 End Using
  2196.  
  2197.             Catch ex As Exception
  2198.                 Throw
  2199.  
  2200.             End Try
  2201.  
  2202.         End If
  2203.  
  2204.     End Function
  2205.  
  2206.     ''' <summary>
  2207.     ''' Modifies the user permissions of a registry key.
  2208.     ''' </summary>
  2209.     ''' <param name="fullKeyPath">The registry key full path.</param>
  2210.     ''' <param name="userAccess">The user access.</param>
  2211.     ''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns>
  2212.     Public Shared Function SetUserAccessKey(ByVal fullKeyPath As String,
  2213.                                             ByVal userAccess() As ReginiUserAccess) As Boolean
  2214.  
  2215.         Return SetUserAccessKey(rootKeyName:=GetRootKeyName(fullKeyPath),
  2216.                                 subKeyPath:=GetSubKeyPath(fullKeyPath),
  2217.                                 userAccess:=userAccess)
  2218.  
  2219.     End Function
  2220.  
  2221. #End Region
  2222.  
  2223. #End Region
  2224.  
  2225. #Region " Private Methods "
  2226.  
  2227. #Region " Collectors "
  2228.  
  2229.     ''' <summary>
  2230.     ''' Collects the subkeys on the specified registry path.
  2231.     ''' </summary>
  2232.     ''' <param name="sourceRegistryKey">The source registry key.</param>
  2233.     ''' <param name="searchOption">The search mode.</param>
  2234.     ''' <returns>IEnumerable(Of System.String).</returns>
  2235.     Private Shared Iterator Function CollectSubKeys(ByVal sourceRegistryKey As RegistryKey,
  2236.                                                     ByVal searchOption As SearchOption) As IEnumerable(Of String)
  2237.  
  2238.         For Each subKeyName As String In sourceRegistryKey.GetSubKeyNames
  2239.  
  2240.             Yield String.Format("{0}\{1}", sourceRegistryKey.Name, subKeyName)
  2241.  
  2242.             If searchOption = searchOption.AllDirectories Then
  2243.  
  2244.                 For Each registryPath As String In CollectSubKeys(sourceRegistryKey.OpenSubKey(subKeyName, writable:=False), searchOption.AllDirectories)
  2245.                     Yield registryPath
  2246.                 Next registryPath
  2247.  
  2248.             End If
  2249.  
  2250.         Next subKeyName
  2251.  
  2252.     End Function
  2253.  
  2254.     ''' <summary>
  2255.     ''' Collects the values on the specified registry path.
  2256.     ''' </summary>
  2257.     ''' <param name="sourceRegistryKey">The source registry key.</param>
  2258.     ''' <param name="searchOption">The search mode.</param>
  2259.     ''' <returns>IEnumerable(Of RegInfo).</returns>
  2260.     Private Shared Iterator Function CollectValues(ByVal sourceRegistryKey As RegistryKey,
  2261.                                                    ByVal searchOption As SearchOption) As IEnumerable(Of RegInfo)
  2262.  
  2263.         Select Case searchOption
  2264.  
  2265.             Case searchOption.TopDirectoryOnly
  2266.                 For Each valueName As String In sourceRegistryKey.GetValueNames
  2267.  
  2268.                     Yield New reginfo With
  2269.                         {
  2270.                             .RootKeyName = GetRootKeyName(sourceRegistryKey.Name),
  2271.                             .SubKeyPath = GetSubKeyPath(sourceRegistryKey.Name),
  2272.                             .ValueName = valueName
  2273.                         }
  2274.  
  2275.                 Next valueName
  2276.  
  2277.             Case searchOption.AllDirectories
  2278.                 For Each registryPath As String In CollectSubKeys(sourceRegistryKey, searchOption)
  2279.  
  2280.                     Dim reg As RegistryKey = GetRootKey(registryPath).OpenSubKey(GetSubKeyPath(registryPath), writable:=False)
  2281.  
  2282.                     For Each valueName As String In reg.GetValueNames
  2283.  
  2284.                         Yield New reginfo With
  2285.                             {
  2286.                                 .RootKeyName = GetRootKeyName(reg.Name),
  2287.                                 .SubKeyPath = GetSubKeyPath(reg.Name),
  2288.                                 .ValueName = valueName
  2289.                             }
  2290.  
  2291.                     Next valueName
  2292.  
  2293.                     reg.Close()
  2294.  
  2295.                 Next registryPath
  2296.  
  2297.         End Select
  2298.  
  2299.     End Function
  2300.  
  2301.     ''' <summary>
  2302.     ''' Collects the value datas on the specified registry path.
  2303.     ''' </summary>
  2304.     ''' <param name="sourceRegistryKey">The source registry key.</param>
  2305.     ''' <param name="searchOption">The search mode.</param>
  2306.     ''' <returns>IEnumerable(Of RegInfo).</returns>
  2307.     Private Shared Iterator Function CollectValueDatas(ByVal sourceRegistryKey As RegistryKey,
  2308.                                                        ByVal searchOption As SearchOption) As IEnumerable(Of RegInfo)
  2309.  
  2310.         Select Case searchOption
  2311.  
  2312.             Case searchOption.TopDirectoryOnly
  2313.                 For Each valueName As String In sourceRegistryKey.GetValueNames
  2314.  
  2315.                     Yield New reginfo With
  2316.                         {
  2317.                             .RootKeyName = GetRootKeyName(sourceRegistryKey.Name),
  2318.                             .SubKeyPath = GetSubKeyPath(sourceRegistryKey.Name),
  2319.                             .ValueName = valueName,
  2320.                             .ValueData = GetValueData(rootKeyName:=.RootKeyName,
  2321.                                                       subKeyPath:=.SubKeyPath,
  2322.                                                       valueName:=.ValueName,
  2323.                                                       registryValueOptions:=RegistryValueOptions.None)
  2324.                         }
  2325.  
  2326.                 Next valueName
  2327.  
  2328.             Case searchOption.AllDirectories
  2329.                 For Each registryPath As String In CollectSubKeys(sourceRegistryKey, searchOption)
  2330.  
  2331.                     Dim reg As RegistryKey = GetRootKey(registryPath).OpenSubKey(GetSubKeyPath(registryPath), writable:=False)
  2332.  
  2333.                     For Each valueName As String In reg.GetValueNames
  2334.  
  2335.                         Yield New reginfo With
  2336.                             {
  2337.                                 .RootKeyName = GetRootKeyName(reg.Name),
  2338.                                 .SubKeyPath = GetSubKeyPath(reg.Name),
  2339.                                 .ValueName = valueName,
  2340.                                 .ValueData = GetValueData(rootKeyName:=.RootKeyName,
  2341.                                                           subKeyPath:=.SubKeyPath,
  2342.                                                           valueName:=.ValueName,
  2343.                                                           registryValueOptions:=RegistryValueOptions.None)
  2344.                             }
  2345.                     Next valueName
  2346.  
  2347.                     reg.Close()
  2348.  
  2349.                 Next registryPath
  2350.  
  2351.         End Select
  2352.  
  2353.     End Function
  2354.  
  2355. #End Region
  2356.  
  2357. #End Region
  2358.  
  2359. End Class
  2360.  
  2361. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement