PageRenderTime 27ms CodeModel.GetById 18ms app.highlight 6ms 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
 20Imports System.Collections.ObjectModel
 21Imports System.Management
 22Imports Microsoft.Win32
 23
 24Friend Class RegistryWatcher
 25    Inherits ManagementEventWatcher
 26    Implements IDisposable
 27
 28    Private Shared _supportedHives As ReadOnlyCollection(Of RegistryKey) = Nothing
 29
 30    ''' <summary>
 31    ''' Changes to the HKEY_CLASSES_ROOT and HKEY_CURRENT_USER hives are not supported
 32    ''' by RegistryEvent or classes derived from it, such as RegistryKeyChangeEvent. 
 33    ''' </summary>
 34    Public Shared ReadOnly Property SupportedHives() As ReadOnlyCollection(Of RegistryKey)
 35        Get
 36            If _supportedHives Is Nothing Then
 37                Dim hives() As RegistryKey = {Registry.LocalMachine, Registry.Users, Registry.CurrentConfig}
 38                _supportedHives = Array.AsReadOnly(Of RegistryKey)(hives)
 39            End If
 40            Return _supportedHives
 41        End Get
 42    End Property
 43
 44
 45    Private _hive As RegistryKey
 46    Public Property Hive() As RegistryKey
 47        Get
 48            Return _hive
 49        End Get
 50        Private Set(ByVal value As RegistryKey)
 51            _hive = value
 52        End Set
 53    End Property
 54
 55    Private _keyPath As String
 56    Public Property KeyPath() As String
 57        Get
 58            Return _keyPath
 59        End Get
 60        Private Set(ByVal value As String)
 61            _keyPath = value
 62        End Set
 63    End Property
 64
 65    Private _keyToMonitor As RegistryKey
 66    Public Property KeyToMonitor() As RegistryKey
 67        Get
 68            Return _keyToMonitor
 69        End Get
 70        Private Set(ByVal value As RegistryKey)
 71            _keyToMonitor = value
 72        End Set
 73    End Property
 74
 75    Public Event RegistryKeyChangeEvent As EventHandler(Of RegistryKeyChangeEventArgs)
 76
 77    ''' <exception cref="System.Security.SecurityException">
 78    ''' Thrown when current user does not have the permission to access the key 
 79    ''' to monitor.
 80    ''' </exception> 
 81    ''' <exception cref="System.ArgumentException">
 82    ''' Thrown when the key to monitor does not exist.
 83    ''' </exception> 
 84    Public Sub New(ByVal hive As RegistryKey, ByVal keyPath As String)
 85        Me.Hive = hive
 86        Me.KeyPath = keyPath
 87
 88        ' If you set the platform of this project to x86 and run it on a 64bit 
 89        ' machine, you will get the Registry Key under 
 90        ' HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node when the key path is
 91        ' HKEY_LOCAL_MACHINE\SOFTWARE
 92        Me.KeyToMonitor = hive.OpenSubKey(keyPath)
 93
 94        If KeyToMonitor IsNot Nothing Then
 95            ' Construct the query string.
 96            Dim queryString As String = String.Format("SELECT * FROM RegistryKeyChangeEvent " & ControlChars.CrLf & "                   WHERE Hive = '{0}' AND KeyPath = '{1}' ", Me.Hive.Name, Me.KeyPath)
 97
 98            Dim query As New WqlEventQuery()
 99            query.QueryString = queryString
100            query.EventClassName = "RegistryKeyChangeEvent"
101            query.WithinInterval = New TimeSpan(0, 0, 0, 1)
102            Me.Query = query
103
104            AddHandler EventArrived, AddressOf RegistryWatcher_EventArrived
105        Else
106            Dim message As String = String.Format("The registry key {0}\{1} does not exist", hive.Name, keyPath)
107            Throw New ArgumentException(message)
108
109        End If
110    End Sub
111
112    Private Sub RegistryWatcher_EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
113
114        ' Get RegistryKeyChangeEventArgs from EventArrivedEventArgs.NewEvent.Properties.
115        Dim args As New RegistryKeyChangeEventArgs(e.NewEvent)
116
117        ' Raise the event handler. 
118        RaiseEvent RegistryKeyChangeEvent(sender, args)
119
120    End Sub
121
122    ''' <summary>
123    ''' Dispose the RegistryKey.
124    ''' </summary>
125    Public Shadows Sub Dispose()
126        MyBase.Dispose()
127        If Me.KeyToMonitor IsNot Nothing Then
128            Me.KeyToMonitor.Close()
129        End If
130    End Sub
131
132End Class