Click here to Skip to main content
15,881,248 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 341.5K   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

 
GeneralThanks so much for this! Pin
Teufel121216-Mar-09 13:02
Teufel121216-Mar-09 13:02 
QuestionHOW TO: get changed key and value Pin
J Sullivan13-Feb-09 20:17
J Sullivan13-Feb-09 20:17 
QuestionHow can I get the changed value and the type? Pin
puyopuy3-Aug-08 19:51
puyopuy3-Aug-08 19:51 
AnswerRe: How can I get the changed value and the type? Pin
Thomas Freudenberg3-Aug-08 20:28
Thomas Freudenberg3-Aug-08 20:28 
GeneralRe: How can I get the changed value and the type? Pin
puyopuy7-Aug-08 1:57
puyopuy7-Aug-08 1:57 
GeneralRe: How can I get the changed value and the type? Pin
Thomas Freudenberg7-Aug-08 2:52
Thomas Freudenberg7-Aug-08 2:52 
GeneralRe: How can I get the changed value and the type? Pin
puyopuy7-Aug-08 15:03
puyopuy7-Aug-08 15:03 
GeneralRe: How can I get the changed value and the type? Pin
nipsonanomimata5-Sep-08 7:55
nipsonanomimata5-Sep-08 7:55 
AnswerRe: How can I get the changed value and the type? Pin
Thomas Freudenberg5-Sep-08 8:22
Thomas Freudenberg5-Sep-08 8:22 
GeneralOnRegChanged - Object Ref not set to instance Pin
Bergeir1-Jul-08 7:24
Bergeir1-Jul-08 7:24 
QuestionMissing files? Pin
tatowaki12-Feb-08 8:54
tatowaki12-Feb-08 8:54 
AnswerRe: Missing files? Pin
Thomas Freudenberg12-Feb-08 9:54
Thomas Freudenberg12-Feb-08 9:54 
Generalthread issues [modified] Pin
TheShaver20-Aug-07 0:10
TheShaver20-Aug-07 0:10 
Questionused in .net 2005? Pin
Paul Pang20-Jul-07 6:59
Paul Pang20-Jul-07 6:59 
AnswerRe: used in .net 2005? Pin
eddewind9-Oct-07 18:15
eddewind9-Oct-07 18:15 
Generalmonitor more precisely Pin
Alexandre GRANVAUD14-May-07 21:54
Alexandre GRANVAUD14-May-07 21:54 
AnswerRe: monitor more precisely Pin
Thomas Freudenberg15-May-07 4:55
Thomas Freudenberg15-May-07 4:55 
GeneralRe: monitor more precisely Pin
jgehman13-Jun-07 3:57
jgehman13-Jun-07 3:57 
AnswerRe: monitor more precisely Pin
Thomas Freudenberg13-Jun-07 4:04
Thomas Freudenberg13-Jun-07 4:04 
GeneralRe: monitor more precisely Pin
GotCodeToo26-Nov-08 10:58
GotCodeToo26-Nov-08 10:58 
GeneralAny way of knowing which process initiated the change Pin
drinkwater11-Apr-07 17:44
drinkwater11-Apr-07 17:44 
GeneralRe: Any way of knowing which process initiated the change Pin
Thomas Freudenberg15-Apr-07 21:43
Thomas Freudenberg15-Apr-07 21:43 
AnswerRe: Any way of knowing which process initiated the change Pin
Vivek_India6-Aug-09 23:54
Vivek_India6-Aug-09 23:54 
GeneralRe: Any way of knowing which process initiated the change [modified] Pin
Thomas Freudenberg7-Aug-09 0:15
Thomas Freudenberg7-Aug-09 0:15 
AnswerRe: Any way of knowing which process initiated the change Pin
Vivek_India9-Aug-09 22:39
Vivek_India9-Aug-09 22:39 

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.