Click here to Skip to main content
15,884,537 members
Articles / Programming Languages / C#

Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET

Rate me:
Please Sign up or sign in to vote.
4.83/5 (17 votes)
14 Nov 2008CPOL6 min read 129.2K   4.5K   70  
How to receive asynchronous events about Registry changes, using WMI.
using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

using Microsoft.Win32;

namespace RegistryMonitor
{
	public class RegistryKeyChange : RegistryChangeBase
	{
		#region Constants

		private const string queryString = "SELECT * FROM RegistryKeyChangeEvent WHERE Hive = '{0}' AND ({1})";

		#endregion Constants

		#region Static Fields

		private static string HiveLocation = "KeyPath";

		#endregion Static Fields

		#region Constructors

        public RegistryKeyChange(string Hive, string KeyPath)
            : this(Hive, new List<string>(new string[] { KeyPath }))
        { }

		public RegistryKeyChange(RegistryKey Hive, string KeyPath)
			: this(Hive.Name, KeyPath)
		{
		}

		public RegistryKeyChange(string Hive, List<string> KeyPathCollection)
			: base(Hive, KeyPathCollection)
		{
			this.Query.QueryString = BuildQueryString(Hive, KeyPathCollection);

			this.EventArrived += new EventArrivedEventHandler(RegistryKeyChange_EventArrived);
		}
        
		public RegistryKeyChange(RegistryKey Hive, List<string> KeyPathCollection)
			: this(Hive.Name, KeyPathCollection)
		{
		}

		#endregion Constructors

		#region Private Methods

		private string BuildQueryString(string Hive, List<string> KeyPathCollection)
		{
			string ORString = RegistryChangeBase.BuildOrString(KeyPathCollection);
			string FormattedOR = string.Format(ORString, HiveLocation);
			return string.Format(queryString, Hive, FormattedOR);
		}

        private void RegistryKeyChange_EventArrived(object sender, EventArrivedEventArgs e)
		{
			RegistryKeyChangeEvent RegValueChange = new RegistryKeyChangeEvent(e.NewEvent);

            OnRegistryKeyChanged(RegValueChange);
		}

        protected virtual void OnRegistryKeyChanged(RegistryKeyChangeEvent RegValueChange)
        {
            if (RegistryKeyChanged != null)
            {
                RegistryKeyChanged(this, new RegistryKeyChangedEventArgs(RegValueChange));
            }
        }

		#endregion Private Methods

		#region Events

		public event EventHandler<RegistryKeyChangedEventArgs> RegistryKeyChanged;

		#endregion Events
	}

    public class RegistryKeyChangedEventArgs : EventArgs
    {
        private RegistryKeyChangeEvent data;

        public RegistryKeyChangeEvent RegistryKeyChangeData
        {
            get
            {
                return data;
            }
        }

        public RegistryKeyChangedEventArgs(RegistryKeyChangeEvent Data)
        {
            data = Data;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions