Advertisement
Guest User

Registry Watcher C#

a guest
Feb 8th, 2012
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. // ---------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="Program.cs" company="">
  3. //
  4. // </copyright>
  5. // <summary>
  6. // Defines the WmiChangeEventTester type.
  7. // </summary>
  8. // ---------------------------------------------------------------------------------------------------------------------
  9. namespace WmiExample
  10. {
  11. using System;
  12. using System.Management;
  13.  
  14. /// <summary>
  15. /// </summary>
  16. public class WmiChangeEventTester
  17. {
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="WmiChangeEventTester"/> class.
  20. /// </summary>
  21. public WmiChangeEventTester()
  22. {
  23. try
  24. {
  25. // Your query goes below; "KeyPath" is the key in the registry that you
  26. // want to monitor for changes. Make sure you escape the character.
  27. WqlEventQuery query = new WqlEventQuery(
  28. "SELECT * FROM RegistryValueChangeEvent WHERE " +
  29. "Hive = 'HKEY_LOCAL_MACHINE'" +
  30. @"AND KeyPath = 'SOFTWARE\Microsoft\.NETFramework' AND ValueName='InstallRoot'");
  31.  
  32. ManagementEventWatcher watcher = new ManagementEventWatcher(query);
  33. Console.WriteLine("Waiting for an event...");
  34.  
  35. // Set up the delegate that will handle the change event.
  36. watcher.EventArrived += new EventArrivedEventHandler(HandleEvent);
  37.  
  38. // Start listening for events.
  39. watcher.Start();
  40.  
  41. // Do something while waiting for events. In your application,
  42. // this would just be continuing business as usual.
  43. System.Threading.Thread.Sleep(100000000);
  44.  
  45. // Stop listening for events.
  46. watcher.Stop();
  47. }
  48. catch (ManagementException managementException)
  49. {
  50. Console.WriteLine("An error occurred: " + managementException.Message);
  51. }
  52. }
  53.  
  54. /// <summary>
  55. /// </summary>
  56. /// <param name="sender">
  57. /// The sender.
  58. /// </param>
  59. /// <param name="e">
  60. /// The e.
  61. /// </param>
  62. private void HandleEvent(object sender, EventArrivedEventArgs e)
  63. {
  64. Console.WriteLine("Received an event.");
  65. // RegistryKeyChangeEvent occurs here; do something.
  66. }
  67.  
  68. /// <summary>
  69. /// </summary>
  70. public static void Main()
  71. {
  72. // Just calls the class above to check for events...
  73. WmiChangeEventTester receiveEvent = new WmiChangeEventTester();
  74. }
  75. }
  76. }
  77.  
  78. public class MonitorSample
  79. {
  80. static void Main()
  81. {
  82. RegistryMonitor monitor = new
  83. RegistryMonitor(RegistryHive.CurrentUser, "Environment");
  84. monitor.RegChanged += new EventHandler(OnRegChanged);
  85. monitor.Start();
  86.  
  87. while(true);
  88.  
  89. monitor.Stop();
  90. }
  91.  
  92. private void OnRegChanged(object sender, EventArgs e)
  93. {
  94. Console.WriteLine("registry key has changed");
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement