// **************************************************************** // Copyright 2002-2003, Charlie Poole // This is free software licensed under the NUnit license. You may // obtain a copy of the license at http://nunit.org/?p=license&r=2.4 // **************************************************************** using System; using System.Diagnostics; using Microsoft.Win32; namespace NUnit.Util { /// /// Implementation of SettingsStorage for NUnit user settings, /// based on storage of settings in the registry. /// /// Setting names containing a dot are interpreted as a /// reference to a subkey. Only the first dot is used /// in this way, since the feature is only intended /// to support legacy registry settings, which are not /// nested any deeper. /// public class RegistrySettingsStorage : ISettingsStorage { #region Instance Variables /// /// If not null, the registry key for this storage /// private RegistryKey storageKey; #endregion #region Construction and Disposal /// /// Construct a storage on top of a pre-created registry key /// /// public RegistrySettingsStorage( RegistryKey storageKey ) { this.storageKey = storageKey; } #endregion #region Properties /// /// The registry key used to hold this storage /// public RegistryKey StorageKey { get { return storageKey; } } #endregion #region ISettingsStorage Members /// /// Load a setting from this storage /// /// Name of the setting to load /// Value of the setting public object GetSetting( string settingName ) { int dot = settingName.IndexOf( '.' ); if ( dot < 0 ) return storageKey.GetValue( settingName ); using( RegistryKey subKey = storageKey.OpenSubKey( settingName.Substring( 0, dot ) ) ) { if ( subKey != null ) return subKey.GetValue( settingName.Substring( dot + 1 ) ); } return null; } /// /// Remove a setting from the storage /// /// Name of the setting to remove public void RemoveSetting( string settingName ) { int dot = settingName.IndexOf( '.' ); if ( dot < 0 ) storageKey.DeleteValue( settingName, false ); else { using( RegistryKey subKey = storageKey.OpenSubKey( settingName.Substring( 0, dot ), true ) ) { if ( subKey != null ) subKey.DeleteValue( settingName.Substring( dot + 1 ) ); } } } public void RemoveGroup( string groupName ) { storageKey.DeleteSubKeyTree( groupName ); } /// /// Save a setting in this storage /// /// Name of the setting to save /// Value to be saved public void SaveSetting( string settingName, object settingValue ) { object val = settingValue; if ( val is bool ) val = ((bool)val) ? 1 : 0; int dot = settingName.IndexOf( '.' ); if ( dot < 0 ) storageKey.SetValue( settingName, val ); else { using( RegistryKey subKey = storageKey.CreateSubKey( settingName.Substring( 0, dot ) ) ) { subKey.SetValue( settingName.Substring( dot + 1 ), val ); } } } /// /// Make a new child storage under this one /// /// Name of the child storage to make /// New storage public ISettingsStorage MakeChildStorage( string storageName ) { return new RegistrySettingsStorage( storageKey.CreateSubKey( storageName ) ); } /// /// LoadSettings does nothing in this implementation, since the /// registry is accessed directly. /// public void LoadSettings() { } /// /// SaveSettings does nothing in this implementation, since the /// registry is accessed directly. /// public void SaveSettings() { } #endregion #region IDisposable Members /// /// Dispose of this object by closing the storage key, if any /// public void Dispose() { if ( storageKey != null ) storageKey.Close(); } #endregion } }