Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#
Article

RegistryMonitor - a .NET wrapper class for RegNotifyChangeKeyValue

Rate me:
Please Sign up or sign in to vote.
4.96/5 (46 votes)
16 Jan 2006CPOL2 min read 340.4K   12.7K   111   92
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:

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

Events

RegistryMonitor supports two events:

C#
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:

C#
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:

C#
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):

C#
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)


Written By
Software Developer Cloud Klabauter GmbH
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMultiple keys Pin
Pasca9213-Jun-19 0:33
Pasca9213-Jun-19 0:33 
QuestionGood starting point. Thanks! Pin
cmohr13-Apr-18 11:12
cmohr13-Apr-18 11:12 
QuestionRunning in a 64 bit environment Pin
Lars Pehrsson7-Feb-18 6:47
Lars Pehrsson7-Feb-18 6:47 
General[My vote of 1] Something happening somewhere Pin
Member 31085637-Sep-15 3:47
Member 31085637-Sep-15 3:47 
QuestionRe: [My vote of 1] Something happening somewhere Pin
Thomas Freudenberg8-Sep-15 23:44
Thomas Freudenberg8-Sep-15 23:44 
AnswerRe: [My vote of 1] Something happening somewhere Pin
Ouroborus7775-Oct-15 6:27
Ouroborus7775-Oct-15 6:27 
What changed and, if possible, what it was changed to. Currently it basically just tells you "something changed somewhere".
GeneralRe: [My vote of 1] Something happening somewhere Pin
Thomas Freudenberg5-Oct-15 10:03
Thomas Freudenberg5-Oct-15 10:03 
QuestionWarning! Does not work for instances where the monitored key may not always exist. Pin
nmg19617-Aug-15 2:13
nmg19617-Aug-15 2:13 
AnswerRe: Warning! Does not work for instances where the monitored key may not always exist. Pin
Thomas Freudenberg8-Sep-15 23:41
Thomas Freudenberg8-Sep-15 23:41 
Questionmuch appriciated, thanks Pin
vdoser16-May-13 8:56
vdoser16-May-13 8:56 
AnswerRe: much appriciated, thanks Pin
Thomas Freudenberg8-Sep-15 23:45
Thomas Freudenberg8-Sep-15 23:45 
Questiontnx Pin
Rohit Dubey from Hyderabad15-Mar-12 5:56
Rohit Dubey from Hyderabad15-Mar-12 5:56 
QuestionGet the process causing the registry key change? Pin
Jon777777718-May-10 8:13
Jon777777718-May-10 8:13 
AnswerRe: Get the process causing the registry key change? Pin
Jon777777718-May-10 9:25
Jon777777718-May-10 9:25 
AnswerRe: Get the process causing the registry key change? Pin
Thomas Freudenberg18-May-10 21:33
Thomas Freudenberg18-May-10 21:33 
QuestionWindows 7??? Pin
psmartt15-Apr-10 17:23
psmartt15-Apr-10 17:23 
AnswerRe: Windows 7??? Pin
psmartt15-Apr-10 19:51
psmartt15-Apr-10 19:51 
QuestionNot Catching RegistryMonitor Events In Windows Service Pin
SeeSharpCutsMe23-Sep-09 9:08
SeeSharpCutsMe23-Sep-09 9:08 
AnswerRe: Not Catching RegistryMonitor Events In Windows Service Pin
Martyn Bone12-Nov-09 0:31
Martyn Bone12-Nov-09 0:31 
GeneralVery-Very Excellent Pin
Anubhava Dimri5-Aug-09 23:49
Anubhava Dimri5-Aug-09 23:49 
GeneralRe: Very-Very Excellent Pin
Thomas Freudenberg6-Aug-09 1:06
Thomas Freudenberg6-Aug-09 1:06 
QuestionWin32Exception: The system cannot find the file specified Pin
Andrew Ensley14-May-09 6:35
Andrew Ensley14-May-09 6:35 
QuestionRe: Win32Exception: The system cannot find the file specified Pin
Thomas Freudenberg16-May-09 7:34
Thomas Freudenberg16-May-09 7:34 
AnswerRe: Win32Exception: The system cannot find the file specified Pin
Member 432876816-Dec-14 18:01
Member 432876816-Dec-14 18:01 
GeneralRe: Win32Exception: The system cannot find the file specified Pin
Opata Chibueze3-Sep-15 20:25
Opata Chibueze3-Sep-15 20:25 

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

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