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
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 RegistryTreeChange : RegistryChangeBase
	{
		#region Constants

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

		#endregion Constants

		#region Static Fields

		private static string HiveLocation = "RootPath";

		#endregion Static Fields

		#region Constructors

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

		public RegistryTreeChange(RegistryKey Hive, string RootPath)
			: this(Hive.Name, RootPath)
		{
		}

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

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

		public RegistryTreeChange(RegistryKey Hive, List<string> RootPathCollection)
			: this(Hive.Name, RootPathCollection)
		{
		}

		#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 RegistryTreeChange_EventArrived(object sender, EventArrivedEventArgs e)
		{
			RegistryTreeChangeEvent RegTreeChange = new RegistryTreeChangeEvent(e.NewEvent);

            OnRegistryTreeChanged(RegTreeChange);
		}

        protected virtual void OnRegistryTreeChanged(RegistryTreeChangeEvent RegTreeChange)
        {
            if (RegistryTreeChanged != null)
            {
                RegistryTreeChanged(this, new RegistryTreeChangedEventArgs(RegTreeChange));
            }
        }

		#endregion Private Methods

		#region Events

		public event EventHandler<RegistryTreeChangedEventArgs> RegistryTreeChanged;

		#endregion Events
	}

    public class RegistryTreeChangedEventArgs : EventArgs
    {
        private RegistryTreeChangeEvent data;

        public RegistryTreeChangeEvent RegistryTreeChangeData
        {
            get
            {
                return data;
            }
        }

        public RegistryTreeChangedEventArgs(RegistryTreeChangeEvent 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 | Mobile
Web03 | 2.8.140814.1 | Last Updated 14 Nov 2008
Article Copyright 2008 by Giorgi Dalakishvili
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid