How to create a windows registry watcher?
Take a look at RegistryMonitor - a .NET wrapper class for RegNotifyChangeKeyValue.
Registry Watcher C#
WMI can sometimes be interesting to work with...I think I understand your question, so take a look at the code snippet below and let me know if it's what you're looking for.
// ---------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="">
//
// </copyright>
// <summary>
// Defines the WmiChangeEventTester type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace WmiExample
{
using System;
using System.Management;
/// <summary>
/// </summary>
public class WmiChangeEventTester
{
/// <summary>
/// Initializes a new instance of the <see cref="WmiChangeEventTester"/> class.
/// </summary>
public WmiChangeEventTester()
{
try
{
// Your query goes below; "KeyPath" is the key in the registry that you
// want to monitor for changes. Make sure you escape the \ character.
WqlEventQuery query = new WqlEventQuery(
"SELECT * FROM RegistryValueChangeEvent WHERE " +
"Hive = 'HKEY_LOCAL_MACHINE'" +
@"AND KeyPath = 'SOFTWARE\\Microsoft\\.NETFramework' AND ValueName='InstallRoot'");
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
Console.WriteLine("Waiting for an event...");
// Set up the delegate that will handle the change event.
watcher.EventArrived += new EventArrivedEventHandler(HandleEvent);
// Start listening for events.
watcher.Start();
// Do something while waiting for events. In your application,
// this would just be continuing business as usual.
System.Threading.Thread.Sleep(100000000);
// Stop listening for events.
watcher.Stop();
}
catch (ManagementException managementException)
{
Console.WriteLine("An error occurred: " + managementException.Message);
}
}
/// <summary>
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
private void HandleEvent(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Received an event.");
// RegistryKeyChangeEvent occurs here; do something.
}
/// <summary>
/// </summary>
public static void Main()
{
// Just calls the class above to check for events...
WmiChangeEventTester receiveEvent = new WmiChangeEventTester();
}
}
}
Registry Watcher C# implement RegistryKeyChangeEvent
After some workaround, I found my way to solve it once and for all with ManagementScope instead of WqlEventQuery:
ManagementScope Scope = new ManagementScope("\\\\.\\root\\default");
EventQuery Query = new EventQuery(@"SELECT * FROM RegistryKeyChangeEvent WHERE Hive='HKEY_LOCAL_MACHINE' AND KeyPath='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall'");
ManagementEventWatcher watcher = new ManagementEventWatcher(Scope, Query);
m_watcher.EventArrived += new EventArrivedEventHandler(RegistryWatcher_EventArrived);
m_watcher.Start();
It seems to be something wrong with the way it parse the query, but now its fine.
How do I monitor registry key using pinvoke regcreatekeyEx in C#?
Try this. It seems to be a pretty popular solution for creating a Registry Monitor:
RegistryMonitor - a .NET wrapper class for RegNotifyChangeKeyValue
Receive notification when RegistryKey Value was changed
I finally solved the problem and got the WMI query version to work:
var currentUser = WindowsIdentity.GetCurrent();
var query = new WqlEventQuery(string.Format(
"SELECT * FROM RegistryValueChangeEvent WHERE Hive='HKEY_USERS' AND KeyPath='{0}\\\\{1}' AND ValueName='{2}'",
currentUser.User.Value, keyPath.Replace("\\","\\\\"), valueName));
_watcher = new ManagementEventWatcher(query);
_watcher.EventArrived += (sender, args) => KeyValueChanged();
_watcher.Start();
I found this "hack" at http://www.codeproject.com/Messages/2844468/Monitoring-HKEY_CURRENT_USER.aspx
Related Topics
MVC 5 Dynamic Rows with Begincollectionitem
Using Libtiff from C# (To Access Tiled Tiff Images)
Async Always Waitingforactivation
How to Use Xpath Function in a Xpathexpression Instance Programatically
C#: How to Edit Items and Subitems in a Listview
How to Install a Windows Font Using C#
Wpf Datagrid Get Selected Cell Value
Selenium: Unable to Access Iframe and Data Inside It
How to Segment the Elements Iterated Over in a Foreach Loop
Sqlite .Net Performance, How to Speed Up Things
How to Check If an Ip Address Is Within a Particular Subnet
How to Draw a Rounded Rectangle in C#
Visualizing an Ast Created with Antlr (In a .Net Environment)
Backslash and Quote in Command-Line Arguments
Calling SQL Defined Function in C#
Combine Two Linq Lambda Expressions
C# Regex: Checking for "A-Z" and "A-Z"
Missing Providername When Debugging Azurefunction as Well as Deploying Azure Function