PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/Visual Studio 2008/VBMonitorRegistryChange/MainForm.vb

#
Visual Basic | 145 lines | 78 code | 23 blank | 44 comment | 0 complexity | 1d750de89333049e130912a7108e007a MD5 | raw file
  1. '*************************** Module Header ******************************\
  2. ' Module Name: MainForm.vb
  3. ' Project: VBMonitorRegistryChange
  4. ' Copyright (c) Microsoft Corporation.
  5. '
  6. ' This is the main form of this application. It is used to initialize the UI and
  7. ' handle the events.
  8. '
  9. ' This source is subject to the Microsoft Public License.
  10. ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  11. ' All other rights reserved.
  12. '
  13. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  14. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  15. ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  16. '**************************************************************************
  17. Imports System.Management
  18. Imports Microsoft.Win32
  19. Partial Public Class MainForm
  20. Inherits Form
  21. ' Current status
  22. Private isMonitoring As Boolean = False
  23. Private watcher As RegistryWatcher = Nothing
  24. Public Sub New()
  25. InitializeComponent()
  26. End Sub
  27. Private Sub MainForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
  28. ' Initialize the data source of cmbHives. Changes to the HKEY_CLASSES_ROOT
  29. ' and HKEY_CURRENT_USER hives are not supported by RegistryEvent or classes
  30. ' derived from it, such as RegistryKeyChangeEvent.
  31. cmbHives.DataSource = RegistryWatcher.SupportedHives
  32. End Sub
  33. ''' <summary>
  34. ''' Handle the click event of btnMonitor.
  35. ''' </summary>
  36. Private Sub btnMonitor_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMonitor.Click
  37. ' If this application is monitoring the registry key, then stop monitoring
  38. ' and enable the editors.
  39. If isMonitoring Then
  40. Dim success As Boolean = StopMonitor()
  41. If success Then
  42. btnMonitor.Text = "Start Monitor"
  43. cmbHives.Enabled = True
  44. tbRegkeyPath.ReadOnly = False
  45. isMonitoring = False
  46. lstChanges.Items.Add(String.Format("{0} Stop monitoring", Date.Now))
  47. End If
  48. ' If this application is idle, then start to monitor and disable the editors.
  49. Else
  50. Dim success As Boolean = StartMonitor()
  51. If success Then
  52. btnMonitor.Text = "Stop Monitor"
  53. cmbHives.Enabled = False
  54. tbRegkeyPath.ReadOnly = True
  55. isMonitoring = True
  56. lstChanges.Items.Add(String.Format("{0} Start monitoring", Date.Now))
  57. End If
  58. End If
  59. End Sub
  60. ''' <summary>
  61. ''' Check whether the key to be monitored exists, and then
  62. ''' start ManagementEventWatcher to watch the RegistryKeyChangeEvent
  63. ''' </summary>
  64. ''' <returns>True if the ManagementEventWatcher starts successfully.</returns>
  65. Private Function StartMonitor() As Boolean
  66. Dim hive As RegistryKey = TryCast(cmbHives.SelectedValue, RegistryKey)
  67. Dim keyPath = tbRegkeyPath.Text.Trim()
  68. Try
  69. watcher = New RegistryWatcher(hive, keyPath)
  70. ' The constructor of RegistryWatcher may throw a SecurityException when
  71. ' the key to monitor does not exist.
  72. Catch _ArgumentException As ArgumentException
  73. MessageBox.Show(_ArgumentException.Message)
  74. Return False
  75. ' The constructor of RegistryWatcher may throw a SecurityException when
  76. ' current user does not have the permission to access the key to monitor.
  77. Catch _SecurityException As System.Security.SecurityException
  78. Dim message As String = String.Format("You do not have permission to access the key {0}\{1}.", hive.Name, keyPath)
  79. MessageBox.Show(message)
  80. Return False
  81. End Try
  82. Try
  83. ' Set up the handler that will handle the change event.
  84. AddHandler watcher.RegistryKeyChangeEvent, AddressOf watcher_RegistryKeyChangeEvent
  85. ' Start listening for events.
  86. watcher.Start()
  87. Return True
  88. Catch comException As System.Runtime.InteropServices.COMException
  89. MessageBox.Show("An error occurred: " & comException.Message)
  90. Return False
  91. Catch managementException_Renamed As ManagementException
  92. MessageBox.Show("An error occurred: " & managementException_Renamed.Message)
  93. Return False
  94. End Try
  95. End Function
  96. ''' <summary>
  97. ''' Stop listening for events.
  98. ''' </summary>
  99. ''' <returns>True if ManagementEventWatcher stops successfully.</returns>
  100. Private Function StopMonitor() As Boolean
  101. Try
  102. watcher.Stop()
  103. Return True
  104. Catch _ManagementException As ManagementException
  105. MessageBox.Show("An error occurred: " & _ManagementException.Message)
  106. Return False
  107. Finally
  108. watcher.Dispose()
  109. End Try
  110. End Function
  111. ''' <summary>
  112. ''' Handle the RegistryKeyChangeEvent.
  113. ''' </summary>
  114. Private Sub watcher_RegistryKeyChangeEvent(ByVal sender As Object, ByVal e As RegistryKeyChangeEventArgs)
  115. Dim newEventMessage As String = String.Format( _
  116. "{0} The key {1}\{2} changed", _
  117. e.TIME_CREATED.ToLocalTime(), _
  118. e.Hive, _
  119. e.KeyPath)
  120. lstChanges.Items.Add(newEventMessage)
  121. End Sub
  122. End Class