Click here to Skip to main content
13,352,428 members (37,712 online)
Click here to Skip to main content
Add your own
alternative version

Stats

245.9K views
8.7K downloads
106 bookmarked
Posted 7 Jul 2003

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)

Share

About the Author

Thomas Freudenberg
Technical Lead Cubeware GmbH
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralRe: Proposal for Enhancements Pin
Thomas Freudenberg27-Sep-06 12:40
memberThomas Freudenberg27-Sep-06 12:40 
GeneralNot able to run multiple instances Pin
sanzay mishra22-Sep-06 8:24
membersanzay mishra22-Sep-06 8:24 
GeneralRe: Not able to run multiple instances Pin
Thomas Freudenberg27-Sep-06 12:38
memberThomas Freudenberg27-Sep-06 12:38 
GeneralRe: Not able to run multiple instances Pin
sanzay mishra29-Sep-06 8:02
membersanzay mishra29-Sep-06 8:02 
GeneralDoesn't fire on Windows 2003 [modified] - it does(!!!) Pin
Michael B. Hansen19-Jul-06 5:22
memberMichael B. Hansen19-Jul-06 5:22 
GeneralRe: Doesn't fire on Windows 2003 Pin
Thomas Freudenberg19-Jul-06 6:13
memberThomas Freudenberg19-Jul-06 6:13 
GeneralRe: Doesn't fire on Windows 2003 Pin
Thomas Freudenberg19-Jul-06 6:20
memberThomas Freudenberg19-Jul-06 6:20 
GeneralRe: Doesn't fire on Windows 2003 Pin
Michael B. Hansen20-Jul-06 21:55
memberMichael B. Hansen20-Jul-06 21:55 
GeneralRe: Doesn't fire on Windows 2003 Pin
host2828-Aug-07 22:43
memberhost2828-Aug-07 22:43 
GeneralProblem getting code working [modified] Pin
jeff_y5-Jul-06 6:53
memberjeff_y5-Jul-06 6:53 
AnswerRe: Problem getting code working Pin
Thomas Freudenberg5-Jul-06 12:36
memberThomas Freudenberg5-Jul-06 12:36 
GeneralRe: Problem getting code working Pin
jeff_y6-Jul-06 4:42
memberjeff_y6-Jul-06 4:42 
Generallittle help please Pin
benneh1-Mar-06 14:39
memberbenneh1-Mar-06 14:39 
AnswerRe: little help please Pin
Thomas Freudenberg1-Mar-06 15:34
memberThomas Freudenberg1-Mar-06 15:34 
Generalcongratulate on your update Pin
JustHistory18-Jan-06 21:27
memberJustHistory18-Jan-06 21:27 
GeneralStop() terminates the event thread Pin
snarum18-Jan-06 2:10
membersnarum18-Jan-06 2:10 
AnswerRe: Stop() terminates the event thread Pin
Thomas Freudenberg18-Jan-06 2:19
memberThomas Freudenberg18-Jan-06 2:19 
General3ks a lot Pin
JustHistory18-Dec-05 21:47
memberJustHistory18-Dec-05 21:47 
AnswerRe: 3ks a lot Pin
Thomas Freudenberg18-Dec-05 22:17
memberThomas Freudenberg18-Dec-05 22:17 
General3ks so much Pin
JustHistory19-Dec-05 14:20
memberJustHistory19-Dec-05 14:20 
AnswerRe: 3ks a lot Pin
Thomas Freudenberg19-Dec-05 21:55
memberThomas Freudenberg19-Dec-05 21:55 
GeneralRe: 3ks a lot Pin
JustHistory20-Dec-05 14:35
memberJustHistory20-Dec-05 14:35 
NewsRe: 3ks a lot Pin
Thomas Freudenberg20-Dec-05 23:37
memberThomas Freudenberg20-Dec-05 23:37 
GeneralThank you for the article!! Pin
HelloNetDevelopers12-Nov-05 14:36
memberHelloNetDevelopers12-Nov-05 14:36 
QuestionChanges made? Pin
stcain18-Nov-04 3:09
memberstcain18-Nov-04 3:09 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 16 Jan 2006
Article Copyright 2003 by Thomas Freudenberg
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid