Click here to Skip to main content
15,885,278 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.3K   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 abstract class RegistryChangeBase : ManagementEventWatcher
	{
		#region Static Fields

		static List<string> validHiveList = new List<string>();
		static string Format = "{{0}} = '{0}' OR ";

		#endregion Static Fields

		#region Constructors

		static RegistryChangeBase()
		{
			string[] validHive = new string[] { "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CURRENT_CONFIG" };
			validHiveList.AddRange(validHive);
		}

		protected RegistryChangeBase(string Hive, List<string> KeyPathCollection)
		{
			ValidateHive(Hive);

            ValidateKeyPathList(KeyPathCollection);

			this.Query.QueryLanguage = "WQL";

            this.Scope.Path.NamespacePath = @"root\default";
		}

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

		#endregion Constructors

		#region Private Methods

		private void ValidateHive(string Hive)
		{
			if (!validHiveList.Contains(Hive.ToUpper()))
			{
			    if (string.IsNullOrEmpty(Hive))
			        throw new ArgumentNullException("Hive", "Hive cannot be null");
			    else
			        throw new ArgumentException("Hive", "Incorrect value");
			}
		}

		private void ValidateKeyPathList(List<string> KeyPathCollection)
		{
            if (KeyPathCollection.Count==0)
            {
                throw new ArgumentNullException("Keypath", "KeyPath cannot be null");
            }

            foreach (string item in KeyPathCollection)
            {
                if (string.IsNullOrEmpty(item))
                {
                    throw new ArgumentNullException("Keypath", "KeyPath cannot be null");
                }
            }
		}

		#endregion Private Methods

		#region Protected Methods

		protected static string BuildOrString(List<string> Values)
		{
			StringBuilder builder = new StringBuilder();
			foreach (string item in Values)
			{
			    builder.Append(string.Format(Format, item));
			}
			builder.Remove(builder.Length - 4, 4);
			return builder.ToString();
		}

		#endregion Protected Methods
	}
}

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