Click here to Skip to main content
11,502,383 members (67,934 online)
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 67.1K 2.6K 65
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 class RegistryValueChange : RegistryChangeBase
	{
		#region Constants

		//private const string queryString = "SELECT * FROM RegistryValueChangeEvent WHERE Hive = '{0}' AND KeyPath = '{1}' AND ValueName = '{2}'";
		private const string queryString = "SELECT * FROM RegistryValueChangeEvent WHERE Hive = '{0}' AND KeyPath = '{1}' AND ({2})";

		#endregion Constants

		#region Static Fields

		private static string HiveLocation = "ValueName";

		#endregion Static Fields

		#region Constructors

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

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

		public RegistryValueChange(string Hive, string KeyPath, List<string> ValueNameCollection)
			: base(Hive, KeyPath)
		{
			foreach (string item in ValueNameCollection)
			{
			    if (string.IsNullOrEmpty(item))
			    {
			        throw new ArgumentNullException("ValueName", "ValueName cannot be null");
			    }
			}

			this.Query.QueryString = BuildQueryString(Hive, KeyPath, ValueNameCollection);
			//string.Format(queryString, Hive, KeyPath, ValueName);

			this.EventArrived += new EventArrivedEventHandler(RegistryValueChange_EventArrived);
		}

		public RegistryValueChange(RegistryKey Hive, string KeyPath, List<string> ValueNameCollection)
			: this(Hive.Name, KeyPath, ValueNameCollection)
		{
		}

		#endregion Constructors

		#region Private Methods

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

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

            OnRegistryValueChanged(RegValueChange);
		}

        protected virtual void OnRegistryValueChanged(RegistryValueChangeEvent RegValueChange)
        {
            if (RegistryValueChanged != null)
            {
                RegistryValueChanged(this, new RegistryValueChangedEventArgs(RegValueChange));
            }
        }

		#endregion Private Methods

		#region Events

		public event EventHandler<RegistryValueChangedEventArgs> RegistryValueChanged;

		#endregion Events
	}

    public class RegistryValueChangedEventArgs : EventArgs
    {
        private RegistryValueChangeEvent data;

        public RegistryValueChangeEvent RegistryValueChangeData
        {
            get
            {
                return data;
            }
        }

        public RegistryValueChangedEventArgs(RegistryValueChangeEvent 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)

Share

About the Author

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

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