Click here to Skip to main content
11,630,144 members (78,914 online)
Click here to Skip to main content

RegistryMonitor - a .NET wrapper class for RegNotifyChangeKeyValue

, 16 Jan 2006 CPOL 207.4K 6.4K 100
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

 
Questionmuch appriciated, thanks Pin
vdoser16-May-13 8:56
membervdoser16-May-13 8:56 
Questiontnx Pin
Rohit Dubey from Hyderabad15-Mar-12 5:56
memberRohit Dubey from Hyderabad15-Mar-12 5:56 
QuestionGet the process causing the registry key change? Pin
Jon777777718-May-10 8:13
memberJon777777718-May-10 8:13 
AnswerRe: Get the process causing the registry key change? Pin
Jon777777718-May-10 9:25
memberJon777777718-May-10 9:25 
AnswerRe: Get the process causing the registry key change? Pin
Thomas Freudenberg18-May-10 21:33
memberThomas Freudenberg18-May-10 21:33 
QuestionWindows 7??? Pin
psmartt15-Apr-10 17:23
memberpsmartt15-Apr-10 17:23 
AnswerRe: Windows 7??? Pin
psmartt15-Apr-10 19:51
memberpsmartt15-Apr-10 19:51 
QuestionNot Catching RegistryMonitor Events In Windows Service Pin
SeeSharpCutsMe23-Sep-09 9:08
memberSeeSharpCutsMe23-Sep-09 9:08 
AnswerRe: Not Catching RegistryMonitor Events In Windows Service Pin
Martyn Bone12-Nov-09 0:31
memberMartyn Bone12-Nov-09 0:31 
GeneralVery-Very Excellent Pin
eg_Anubhava5-Aug-09 23:49
membereg_Anubhava5-Aug-09 23:49 
GeneralRe: Very-Very Excellent Pin
Thomas Freudenberg6-Aug-09 1:06
memberThomas Freudenberg6-Aug-09 1:06 
QuestionWin32Exception: The system cannot find the file specified Pin
Member 316774814-May-09 6:35
memberMember 316774814-May-09 6:35 
QuestionRe: Win32Exception: The system cannot find the file specified Pin
Thomas Freudenberg16-May-09 7:34
memberThomas Freudenberg16-May-09 7:34 
AnswerRe: Win32Exception: The system cannot find the file specified Pin
Member 432876816-Dec-14 18:01
memberMember 432876816-Dec-14 18:01 
GeneralThanks so much for this! Pin
Teufel121216-Mar-09 13:02
memberTeufel121216-Mar-09 13:02 
QuestionHOW TO: get changed key and value Pin
Jared James Sullivan13-Feb-09 20:17
memberJared James Sullivan13-Feb-09 20:17 
GeneralOnRegChanged - Object Ref not set to instance Pin
Bergeir1-Jul-08 7:24
memberBergeir1-Jul-08 7:24 
QuestionMissing files? Pin
tatowaki12-Feb-08 8:54
membertatowaki12-Feb-08 8:54 
Cannot download the attached files.
Are they missing?

Thanks
AnswerRe: Missing files? Pin
Thomas Freudenberg12-Feb-08 9:54
memberThomas Freudenberg12-Feb-08 9:54 
Generalthread issues [modified] Pin
TheShaver20-Aug-07 0:10
memberTheShaver20-Aug-07 0:10 
Questionused in .net 2005? Pin
Paul Pang20-Jul-07 6:59
memberPaul Pang20-Jul-07 6:59 
AnswerRe: used in .net 2005? Pin
eddewind9-Oct-07 18:15
membereddewind9-Oct-07 18:15 
Generalmonitor more precisely Pin
Alexandre GRANVAUD14-May-07 21:54
memberAlexandre GRANVAUD14-May-07 21:54 
AnswerRe: monitor more precisely Pin
Thomas Freudenberg15-May-07 4:55
memberThomas Freudenberg15-May-07 4:55 
GeneralRe: monitor more precisely Pin
jgehman13-Jun-07 3:57
memberjgehman13-Jun-07 3:57 
AnswerRe: monitor more precisely Pin
Thomas Freudenberg13-Jun-07 4:04
memberThomas Freudenberg13-Jun-07 4:04 
GeneralRe: monitor more precisely Pin
N_Nguyen26-Nov-08 10:58
memberN_Nguyen26-Nov-08 10:58 
GeneralAny way of knowing which process initiated the change Pin
drinkwater11-Apr-07 17:44
memberdrinkwater11-Apr-07 17:44 
GeneralRe: Any way of knowing which process initiated the change Pin
Thomas Freudenberg15-Apr-07 21:43
memberThomas Freudenberg15-Apr-07 21:43 
AnswerRe: Any way of knowing which process initiated the change Pin
Vivek_India6-Aug-09 23:54
memberVivek_India6-Aug-09 23:54 
GeneralRe: Any way of knowing which process initiated the change [modified] Pin
Thomas Freudenberg7-Aug-09 0:15
memberThomas Freudenberg7-Aug-09 0:15 
AnswerRe: Any way of knowing which process initiated the change Pin
Vivek_India9-Aug-09 22:39
memberVivek_India9-Aug-09 22:39 
Question~(*|*)~How can I monitor Registry for a special process? Pin
behnam_iut14-Dec-06 10:28
memberbehnam_iut14-Dec-06 10:28 
AnswerRe: ~(*|*)~How can I monitor Registry for a special process? Pin
Thomas Freudenberg14-Dec-06 10:31
memberThomas Freudenberg14-Dec-06 10:31 
GeneralProposal for Enhancements Pin
LJEstrada25-Sep-06 5:19
memberLJEstrada25-Sep-06 5:19 
GeneralRe: Proposal for Enhancements Pin
Thomas Freudenberg27-Sep-06 11:40
memberThomas Freudenberg27-Sep-06 11:40 
GeneralNot able to run multiple instances Pin
sanzay mishra22-Sep-06 7:24
membersanzay mishra22-Sep-06 7:24 
GeneralRe: Not able to run multiple instances Pin
Thomas Freudenberg27-Sep-06 11:38
memberThomas Freudenberg27-Sep-06 11:38 
GeneralRe: Not able to run multiple instances Pin
sanzay mishra29-Sep-06 7:02
membersanzay mishra29-Sep-06 7:02 
GeneralDoesn't fire on Windows 2003 [modified] - it does(!!!) Pin
Michael B. Hansen19-Jul-06 4:22
memberMichael B. Hansen19-Jul-06 4:22 
GeneralRe: Doesn't fire on Windows 2003 Pin
Thomas Freudenberg19-Jul-06 5:13
memberThomas Freudenberg19-Jul-06 5:13 
GeneralRe: Doesn't fire on Windows 2003 Pin
Thomas Freudenberg19-Jul-06 5:20
memberThomas Freudenberg19-Jul-06 5:20 
GeneralRe: Doesn't fire on Windows 2003 Pin
Michael B. Hansen20-Jul-06 20:55
memberMichael B. Hansen20-Jul-06 20:55 
GeneralRe: Doesn't fire on Windows 2003 Pin
host2828-Aug-07 21:43
memberhost2828-Aug-07 21:43 
GeneralProblem getting code working [modified] Pin
jeff_y5-Jul-06 5:53
memberjeff_y5-Jul-06 5:53 
AnswerRe: Problem getting code working Pin
Thomas Freudenberg5-Jul-06 11:36
memberThomas Freudenberg5-Jul-06 11:36 
GeneralRe: Problem getting code working Pin
jeff_y6-Jul-06 3:42
memberjeff_y6-Jul-06 3:42 
Generallittle help please Pin
benneh1-Mar-06 13:39
memberbenneh1-Mar-06 13:39 
AnswerRe: little help please Pin
Thomas Freudenberg1-Mar-06 14:34
memberThomas Freudenberg1-Mar-06 14:34 
Generalcongratulate on your update Pin
JustHistory18-Jan-06 20:27
memberJustHistory18-Jan-06 20:27 

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 | Terms of Use | Mobile
Web01 | 2.8.150723.1 | Last Updated 16 Jan 2006
Article Copyright 2003 by Thomas Freudenberg
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid