Click here to Skip to main content
12,401,832 members (60,865 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 RegistryKeyChange : RegistryChangeBase
	{
		#region Constants

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

		#endregion Constants

		#region Static Fields

		private static string HiveLocation = "KeyPath";

		#endregion Static Fields

		#region Constructors

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

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

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

			this.EventArrived += new EventArrivedEventHandler(RegistryKeyChange_EventArrived);
		}
        
		public RegistryKeyChange(RegistryKey Hive, List<string> KeyPathCollection)
			: this(Hive.Name, KeyPathCollection)
		{
		}

		#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 RegistryKeyChange_EventArrived(object sender, EventArrivedEventArgs e)
		{
			RegistryKeyChangeEvent RegValueChange = new RegistryKeyChangeEvent(e.NewEvent);

            OnRegistryKeyChanged(RegValueChange);
		}

        protected virtual void OnRegistryKeyChanged(RegistryKeyChangeEvent RegValueChange)
        {
            if (RegistryKeyChanged != null)
            {
                RegistryKeyChanged(this, new RegistryKeyChangedEventArgs(RegValueChange));
            }
        }

		#endregion Private Methods

		#region Events

		public event EventHandler<RegistryKeyChangedEventArgs> RegistryKeyChanged;

		#endregion Events
	}

    public class RegistryKeyChangedEventArgs : EventArgs
    {
        private RegistryKeyChangeEvent data;

        public RegistryKeyChangeEvent RegistryKeyChangeData
        {
            get
            {
                return data;
            }
        }

        public RegistryKeyChangedEventArgs(RegistryKeyChangeEvent 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