Click here to Skip to main content
12,404,178 members (77,827 online)
Click here to Skip to main content

Stats

74.2K views
3K downloads
66 bookmarked
Posted

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

, 14 Nov 2008 CPOL
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 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

You may also be interested in...

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