Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 14 Nov 2008 CPOL
How to receive asynchronous events about Registry changes, using WMI.
RegistryMonitor_demo.zip
RegistryMonitor_demo
RegistryMonitor.dll
RegistryMonitorSampleApplication.exe
RegistryMonitor_src.zip
RegistryMonitor_src
RegistryMonitor
RegistryMonitor
bin
Debug
Release
Properties
WMI classes
RegistryMonitorSampleApplication
bin
Debug
Release
Properties
Settings.settings
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)

Share

About the Author

Giorgi Dalakishvili
Software Developer
Georgia Georgia
No Biography provided
Follow on   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 14 Nov 2008
Article Copyright 2008 by Giorgi Dalakishvili
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid