#region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig /************************************************************************************ ' ' Copyright 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole ' Copyright 2000-2002 Philip A. Craig ' ' This software is provided 'as-is', without any express or implied warranty. In no ' event will the authors be held liable for any damages arising from the use of this ' software. ' ' Permission is granted to anyone to use this software for any purpose, including ' commercial applications, and to alter it and redistribute it freely, subject to the ' following restrictions: ' ' 1. The origin of this software must not be misrepresented; you must not claim that ' you wrote the original software. If you use this software in a product, an ' acknowledgment (see the following) in the product documentation is required. ' ' Portions Copyright 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole ' or Copyright 2000-2002 Philip A. Craig ' ' 2. Altered source versions must be plainly marked as such, and must not be ' misrepresented as being the original software. ' ' 3. This notice may not be removed or altered from any source distribution. ' '***********************************************************************************/ #endregion 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. /// public class RegistrySettingsStorage : SettingsStorage, IDisposable { #region Instance Variables /// /// If not null, the registry key for this storage /// private RegistryKey storageKey; #endregion #region Construction and Disposal /// /// Construct a storage as a child of another storage /// /// The name to give the storage /// The parent in which the storage is to be created public RegistrySettingsStorage( string storageName, RegistrySettingsStorage parentStorage ) : base( storageName, parentStorage ) { this.storageKey = parentStorage.StorageKey.CreateSubKey( storageName ); } /// /// Construct a storage using a registry key. This constructor is /// intended for use at the top level of the hierarchy. /// /// The name to give the storage /// The registry Key under which the storage will be created public RegistrySettingsStorage( string storageName, RegistryKey parentKey ) : base ( storageName, null ) { this.storageKey = parentKey.CreateSubKey( storageName ); } /// /// Construct a storage on top of a given key, using the key's name /// /// public RegistrySettingsStorage( RegistryKey storageKey ) : base( storageKey.Name, null ) { this.storageKey = storageKey; } /// /// Dispose of this object by closing the storage key, if any /// public override void Dispose() { if ( storageKey != null ) storageKey.Close(); } #endregion #region Properties /// /// The registry key used to hold this storage /// public RegistryKey StorageKey { get { return storageKey; } } /// /// The count of settings in this storage /// public override int SettingsCount { get { return storageKey.ValueCount; } } #endregion #region Methods /// /// Find out if a child storage exists /// /// Name of the child storage /// True if the child storage exists public override bool ChildStorageExists( string storageName ) { using (RegistryKey key = storageKey.OpenSubKey( storageName ) ) { return key != null; } } /// /// Make a new child storage under this one /// /// Name of the child storage to make /// New storage public override SettingsStorage MakeChildStorage( string storageName ) { return new RegistrySettingsStorage( storageName, this ); } /// /// Load a setting from this storage /// /// Name of the setting to load /// Value of the setting public override object LoadSetting( string settingName ) { return storageKey.GetValue( settingName ); } /// /// Load an int setting from this storage. Since int is a /// value type, we can't return null so zero is used to /// indicate that nothing was found - or the found value /// was zero. If you need to distinguish, use your own /// default value or call LoadSetting and check for null. /// /// Name of the setting to load /// Value of the setting or zero if missing public override int LoadIntSetting( string settingName ) { return LoadIntSetting( settingName, 0 ); } /// /// Load a string setting from this storage /// /// Name of the setting to load /// Value of the setting public override string LoadStringSetting( string settingName ) { object resultValue = storageKey.GetValue( settingName ); if ( resultValue == null || resultValue is string ) return (string) resultValue; return resultValue.ToString(); } /// /// Load a setting from this storage or return a default value /// /// Name of setting to load /// Value to return if the seeting is not present /// Value of the setting or the default public override object LoadSetting( string settingName, object defaultValue ) { return storageKey.GetValue( settingName, defaultValue ); } /// /// Load an integer setting from this storage or return a default value /// /// Name of setting to load /// Value to return if the seeting is not present /// Value of the setting or the default public override int LoadIntSetting( string settingName, int defaultValue ) { object resultValue = storageKey.GetValue( settingName, defaultValue ); if ( resultValue is int ) return (int)resultValue; return int.Parse( (string)resultValue ); } /// /// Load a string setting from this storage or return a default value /// /// Name of setting to load /// Value to return if the seeting is not present /// Value of the setting or the default public override string LoadStringSetting( string settingName, string defaultValue ) { object resultValue = storageKey.GetValue( settingName, defaultValue ); if ( resultValue is string ) return (string) resultValue; return resultValue.ToString(); } /// /// Remove a setting from the storage /// /// Name of the setting to remove public override void RemoveSetting( string settingName ) { storageKey.DeleteValue( settingName, false ); } /// /// Save a setting in this storage /// /// Name of the setting to save /// Value to be saved public override void SaveSetting( string settingName, object settingValue ) { storageKey.SetValue( settingName, settingValue ); } /// /// Static function that clears out the contents of a key /// /// Key to be cleared public static void ClearKey( RegistryKey key ) { foreach( string name in key.GetValueNames() ) key.DeleteValue( name ); foreach( string name in key.GetSubKeyNames() ) key.DeleteSubKeyTree( name ); } /// /// Clear all settings from the storage - empty storage remains /// public override void Clear() { ClearKey( storageKey ); } #endregion } }