Click here to Skip to main content
6,629,885 members and growing! (21,509 online)
Email Password   helpLost your password?
General Reading » Hardware & System » Registry     Intermediate License: The Code Project Open License (CPOL)

RegistryMonitor - a .NET wrapper class for RegNotifyChangeKeyValue

By Thomas Freudenberg

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.
C#, Windows, .NET 1.0, .NET 1.1VS.NET2003, Dev
Posted:7 Jul 2003
Updated:16 Jan 2006
Views:109,752
Bookmarked:88 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
32 votes for this article.
Popularity: 7.25 Rating: 4.81 out of 5
1 vote, 3.1%
1

2
1 vote, 3.1%
3
3 votes, 9.4%
4
27 votes, 84.4%
5

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


Member

Occupation: Architect
Company: Pharmatechnik GmbH & Co. KG
Location: Germany Germany

Other popular Hardware & System articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 72 (Total in Forum: 72) (Refresh)FirstPrevNext
QuestionNot Catching RegistryMonitor Events In Windows Service PinmemberSeeSharpCutsMe10:08 23 Sep '09  
AnswerRe: Not Catching RegistryMonitor Events In Windows Service PinmemberMartyn Bone1:31 12 Nov '09  
GeneralVery-Very Excellent Pinmembereg_Anubhava0:49 6 Aug '09  
GeneralRe: Very-Very Excellent PinmemberThomas Freudenberg2:06 6 Aug '09  
QuestionWin32Exception: The system cannot find the file specified PinmemberMember 31677487:35 14 May '09  
QuestionRe: Win32Exception: The system cannot find the file specified PinmemberThomas Freudenberg8:34 16 May '09  
GeneralThanks so much for this! PinmemberTeufel121214:02 16 Mar '09  
GeneralHOW TO: get changed key and value PinmemberJared James Sullivan21:17 13 Feb '09  
QuestionHow can I get the changed value and the type? Pinmemberpuyopuy20:51 3 Aug '08  
AnswerRe: How can I get the changed value and the type? PinmemberThomas Freudenberg21:28 3 Aug '08  
GeneralRe: How can I get the changed value and the type? Pinmemberpuyopuy2:57 7 Aug '08  
GeneralRe: How can I get the changed value and the type? PinmemberThomas Freudenberg3:52 7 Aug '08  
GeneralRe: How can I get the changed value and the type? Pinmemberpuyopuy16:03 7 Aug '08  
GeneralRe: How can I get the changed value and the type? Pinmembernipsonanomimata8:55 5 Sep '08  
AnswerRe: How can I get the changed value and the type? PinmemberThomas Freudenberg9:22 5 Sep '08  
GeneralOnRegChanged - Object Ref not set to instance PinmemberBergeir8:24 1 Jul '08  
GeneralMissing files? Pinmembertatowaki9:54 12 Feb '08  
AnswerRe: Missing files? PinmemberThomas Freudenberg10:54 12 Feb '08  
Generalthread issues [modified] PinmemberTheShaver1:10 20 Aug '07  
Generalused in .net 2005? PinmemberPaul Pang7:59 20 Jul '07  
GeneralRe: used in .net 2005? Pinmembereddewind19:15 9 Oct '07  
Generalmonitor more precisely PinmemberAlexandre GRANVAUD22:54 14 May '07  
AnswerRe: monitor more precisely PinmemberThomas Freudenberg5:55 15 May '07  
GeneralRe: monitor more precisely Pinmemberjgehman4:57 13 Jun '07  
AnswerRe: monitor more precisely PinmemberThomas Freudenberg5:04 13 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Jan 2006
Editor: Smitha Vijayan
Copyright 2003 by Thomas Freudenberg
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project