Click here to Skip to main content
Click here to Skip to main content

RegistryMonitor - a .NET wrapper class for RegNotifyChangeKeyValue

, 16 Jan 2006
Rate this:
Please Sign up or sign in to vote.
The Windows API provides a function RegNotifyChangeKeyValue, which is not covered by the Microsoft.Win32.RegistryKey class. This solution imports that function and encapsulates it in a convenient manner.

Sample Image - RegistryMonitor.png

Introduction

The Windows API provides a function RegNotifyChangeKeyValue, which notifies the caller about changes to attributes or the content of a specified registry key. Unfortunately, this function is not provided by the Microsoft.Win32.RegistryKey class. Because I needed that functionality, I've written a simple wrapper class.

Usage

Instantiation

RegistryMonitor has three constructors, the parameter lists should be self-explanatory:

RegistryMonitor(RegistryKey registryKey)
RegistryMonitor(string name)
RegistryMonitor(RegistryHive registryHive, string subKey)

Events

RegistryMonitor supports two events:

public event EventHandler RegChanged;
public event ErrorEventHandler Error;

The RegChanged event is raised when the registry key specified during construction has changed. Error is raised when an exception occurs.

The latter event is necessary because the monitoring is hosted in a different thread.

Properties

RegistryMonitor has only one property:

public RegChangeNotifyFilter RegChangeNotifyFilter { get; set; }

RegChangeNotifyFilter is an enum. Since I don't want to repeat its implementation here in the article, I just want to say that it controls which kinds of registry changes will be detected, e.g. only key or value changes.

Methods

RegistryMonitor has two public methods which are declared as follows:

public void Start();
public void Stop();

I don't think that these methods require much explanation. The former creates a separate thread, which will monitor the registry, and the latter will stop that thread.

Example

Because a simple example will say more than a thousand words, here's a console sample monitoring HKCU\Environment (that's where the current user's environment variables are stored):

public class MonitorSample
{
    static void Main() 
    {
        RegistryMonitor monitor = new 
          RegistryMonitor(RegistryHive.CurrentUser, "Environment");
        monitor.RegChanged += new EventHandler(OnRegChanged);
        monitor.Start();
 
        while(true);
        
        monitor.Stop();
    }
         
    private void OnRegChanged(object sender, EventArgs e)
    {
        Console.WriteLine("registry key has changed");
    }
}

Provided with this article is another demo, which is a WinForms application (however, a VS.NET 2003 solution).

Points of interest

The first version of RegistryMonitor used reflection to retrieve the private hkey field of the Microsoft.Win32.RegistryKey class. However, Microsoft changed the internal implementation, so this hack didn't work anymore (see the comments below). Therefore I changed my implementation, so that RegistryMonitor now uses P/Invoke to open and close the registry key explicitly.

History

  • 08-Jul-2003 - Initial release.
  • 15-Jan-2005 - Updated to work for both .NET 1.1 and 2.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Thomas Freudenberg
Architect Pharmatechnik GmbH & Co. KG
Germany Germany
No Biography provided
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
Questionmuch appriciated, thanks Pinmembervdoser16-May-13 8:56 
Questiontnx PinmemberRohit Dubey from Hyderabad15-Mar-12 5:56 
QuestionGet the process causing the registry key change? PinmemberJon777777718-May-10 8:13 
AnswerRe: Get the process causing the registry key change? PinmemberJon777777718-May-10 9:25 
AnswerRe: Get the process causing the registry key change? PinmemberThomas Freudenberg18-May-10 21:33 
QuestionWindows 7??? Pinmemberpsmartt15-Apr-10 17:23 
AnswerRe: Windows 7??? Pinmemberpsmartt15-Apr-10 19:51 
QuestionNot Catching RegistryMonitor Events In Windows Service PinmemberSeeSharpCutsMe23-Sep-09 9:08 
AnswerRe: Not Catching RegistryMonitor Events In Windows Service PinmemberMartyn Bone12-Nov-09 0:31 
You are monitoring a key inside HKEY_CURRENT_USER
 
If you are running the app as a desktop app it will be monitor the key in the presently logged on users registry hive.
 
If you are running the app as a service it will be looking in the registry hive of the service user account, not the logged on user.
By default this will be the
HKEY_USERS\S-1-5-18 for local system
HKEY_USERS\S-1-5-19 for local service
HKEY_USERS\S-1-5-20 for network service
 
If you are trying to monitor the logged on users keys from a service, you need to do something like getting the sids of the logged on users (or users) or monitoring the windows logon events, and then monitoring from you service HKEY_USERS\.
Note if you do this you need ot be extremly careful that you detect user session ends and removing the monitoring, otherwise it can prevent things like roaming profile uploads at logoff as you have the users registry hive locked.
 
-
Drayath
GeneralVery-Very Excellent Pinmembereg_Anubhava5-Aug-09 23:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 16 Jan 2006
Article Copyright 2003 by Thomas Freudenberg
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid