PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/VBMonitorRegistryChange/RegistryWatcher.vb

#
Visual Basic | 132 lines | 72 code | 21 blank | 39 comment | 0 complexity | b9dab610073455ccb3e54da0d0ee8d23 MD5 | raw file
  1. '*************************** Module Header ******************************\
  2. ' Module Name: RegistryWatcher.vb
  3. ' Project: VBMonitorRegistryChange
  4. ' Copyright (c) Microsoft Corporation.
  5. '
  6. ' This class derived from ManagementEventWatcher. It is used to
  7. ' 1. Supply the supported hives.
  8. ' 2. Construct a WqlEventQuery from Hive and KeyPath.
  9. ' 3. Wrap the EventArrivedEventArgs to RegistryKeyChangeEventArg.
  10. '
  11. ' This source is subject to the Microsoft Public License.
  12. ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  13. ' All other rights reserved.
  14. '
  15. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  16. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  17. ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  18. '**************************************************************************
  19. Imports System.Collections.ObjectModel
  20. Imports System.Management
  21. Imports Microsoft.Win32
  22. Friend Class RegistryWatcher
  23. Inherits ManagementEventWatcher
  24. Implements IDisposable
  25. Private Shared _supportedHives As ReadOnlyCollection(Of RegistryKey) = Nothing
  26. ''' <summary>
  27. ''' Changes to the HKEY_CLASSES_ROOT and HKEY_CURRENT_USER hives are not supported
  28. ''' by RegistryEvent or classes derived from it, such as RegistryKeyChangeEvent.
  29. ''' </summary>
  30. Public Shared ReadOnly Property SupportedHives() As ReadOnlyCollection(Of RegistryKey)
  31. Get
  32. If _supportedHives Is Nothing Then
  33. Dim hives() As RegistryKey = {Registry.LocalMachine, Registry.Users, Registry.CurrentConfig}
  34. _supportedHives = Array.AsReadOnly(Of RegistryKey)(hives)
  35. End If
  36. Return _supportedHives
  37. End Get
  38. End Property
  39. Private _hive As RegistryKey
  40. Public Property Hive() As RegistryKey
  41. Get
  42. Return _hive
  43. End Get
  44. Private Set(ByVal value As RegistryKey)
  45. _hive = value
  46. End Set
  47. End Property
  48. Private _keyPath As String
  49. Public Property KeyPath() As String
  50. Get
  51. Return _keyPath
  52. End Get
  53. Private Set(ByVal value As String)
  54. _keyPath = value
  55. End Set
  56. End Property
  57. Private _keyToMonitor As RegistryKey
  58. Public Property KeyToMonitor() As RegistryKey
  59. Get
  60. Return _keyToMonitor
  61. End Get
  62. Private Set(ByVal value As RegistryKey)
  63. _keyToMonitor = value
  64. End Set
  65. End Property
  66. Public Event RegistryKeyChangeEvent As EventHandler(Of RegistryKeyChangeEventArgs)
  67. ''' <exception cref="System.Security.SecurityException">
  68. ''' Thrown when current user does not have the permission to access the key
  69. ''' to monitor.
  70. ''' </exception>
  71. ''' <exception cref="System.ArgumentException">
  72. ''' Thrown when the key to monitor does not exist.
  73. ''' </exception>
  74. Public Sub New(ByVal hive As RegistryKey, ByVal keyPath As String)
  75. Me.Hive = hive
  76. Me.KeyPath = keyPath
  77. ' If you set the platform of this project to x86 and run it on a 64bit
  78. ' machine, you will get the Registry Key under
  79. ' HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node when the key path is
  80. ' HKEY_LOCAL_MACHINE\SOFTWARE
  81. Me.KeyToMonitor = hive.OpenSubKey(keyPath)
  82. If KeyToMonitor IsNot Nothing Then
  83. ' Construct the query string.
  84. Dim queryString As String = String.Format("SELECT * FROM RegistryKeyChangeEvent " & ControlChars.CrLf & " WHERE Hive = '{0}' AND KeyPath = '{1}' ", Me.Hive.Name, Me.KeyPath)
  85. Dim query As New WqlEventQuery()
  86. query.QueryString = queryString
  87. query.EventClassName = "RegistryKeyChangeEvent"
  88. query.WithinInterval = New TimeSpan(0, 0, 0, 1)
  89. Me.Query = query
  90. AddHandler EventArrived, AddressOf RegistryWatcher_EventArrived
  91. Else
  92. Dim message As String = String.Format("The registry key {0}\{1} does not exist", hive.Name, keyPath)
  93. Throw New ArgumentException(message)
  94. End If
  95. End Sub
  96. Private Sub RegistryWatcher_EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
  97. ' Get RegistryKeyChangeEventArgs from EventArrivedEventArgs.NewEvent.Properties.
  98. Dim args As New RegistryKeyChangeEventArgs(e.NewEvent)
  99. ' Raise the event handler.
  100. RaiseEvent RegistryKeyChangeEvent(sender, args)
  101. End Sub
  102. ''' <summary>
  103. ''' Dispose the RegistryKey.
  104. ''' </summary>
  105. Public Shadows Sub Dispose()
  106. MyBase.Dispose()
  107. If Me.KeyToMonitor IsNot Nothing Then
  108. Me.KeyToMonitor.Close()
  109. End If
  110. End Sub
  111. End Class