PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/src/NUnit/util/RegistrySettingsStorage.cs

#
C# | 167 lines | 89 code | 23 blank | 55 comment | 10 complexity | 14060552e268dfeda756a9a5068ed892 MD5 | raw file
  1// ****************************************************************
  2// Copyright 2002-2003, Charlie Poole
  3// This is free software licensed under the NUnit license. You may
  4// obtain a copy of the license at http://nunit.org
  5// ****************************************************************
  6
  7using System;
  8using System.Diagnostics;
  9using Microsoft.Win32;
 10
 11namespace NUnit.Util
 12{
 13	/// <summary>
 14	/// Implementation of SettingsStorage for NUnit user settings,
 15	/// based on storage of settings in the registry.
 16	/// 
 17	/// Setting names containing a dot are interpreted as a 
 18	/// reference to a subkey. Only the first dot is used
 19	/// in this way, since the feature is only intended
 20	/// to support legacy registry settings, which are not
 21	/// nested any deeper.
 22	/// </summary>
 23	public class RegistrySettingsStorage : ISettingsStorage
 24	{
 25		#region Instance Variables
 26
 27		/// <summary>
 28		/// If not null, the registry key for this storage
 29		/// </summary>
 30		private RegistryKey storageKey;
 31
 32		#endregion
 33
 34		#region Construction and Disposal
 35
 36		/// <summary>
 37		/// Construct a storage on top of a pre-created registry key
 38		/// </summary>
 39		/// <param name="storageKey"></param>
 40		public RegistrySettingsStorage( RegistryKey storageKey )
 41		{
 42			this.storageKey = storageKey;
 43		}
 44
 45		#endregion
 46
 47		#region Properties
 48
 49		/// <summary>
 50		/// The registry key used to hold this storage
 51		/// </summary>
 52		public RegistryKey StorageKey
 53		{
 54			get { return storageKey; }
 55		}
 56
 57		#endregion
 58
 59		#region ISettingsStorage Members
 60
 61		/// <summary>
 62		/// Load a setting from this storage
 63		/// </summary>
 64		/// <param name="settingName">Name of the setting to load</param>
 65		/// <returns>Value of the setting</returns>
 66		public object GetSetting( string settingName )
 67		{
 68			int dot = settingName.IndexOf( '.' );
 69			if ( dot < 0 )
 70				return storageKey.GetValue( settingName );
 71
 72			using( RegistryKey subKey = storageKey.OpenSubKey( settingName.Substring( 0, dot ) ) )
 73			{
 74				if ( subKey != null )
 75					return subKey.GetValue( settingName.Substring( dot + 1 ) );
 76			}
 77
 78			return null;
 79		}
 80
 81		/// <summary>
 82		/// Remove a setting from the storage
 83		/// </summary>
 84		/// <param name="settingName">Name of the setting to remove</param>
 85		public void RemoveSetting( string settingName )
 86		{
 87			int dot = settingName.IndexOf( '.' );
 88			if ( dot < 0 )
 89				storageKey.DeleteValue( settingName, false );
 90			else
 91			{
 92				using( RegistryKey subKey = storageKey.OpenSubKey( settingName.Substring( 0, dot ), true ) )
 93				{
 94					if ( subKey != null )
 95						subKey.DeleteValue( settingName.Substring( dot + 1 ) );
 96				}
 97			}
 98		}
 99
100		public void RemoveGroup( string groupName )
101		{
102			storageKey.DeleteSubKeyTree( groupName );
103		}
104
105		/// <summary>
106		/// Save a setting in this storage
107		/// </summary>
108		/// <param name="settingName">Name of the setting to save</param>
109		/// <param name="settingValue">Value to be saved</param>
110		public void SaveSetting( string settingName, object settingValue )
111		{
112			object val = settingValue;
113			if ( val is bool )
114				val = ((bool)val) ? 1 : 0;
115
116			int dot = settingName.IndexOf( '.' );
117			if ( dot < 0 )
118				storageKey.SetValue( settingName, val );
119			else
120			{
121				using(  RegistryKey subKey = storageKey.CreateSubKey( settingName.Substring( 0, dot ) ) )
122				{
123					subKey.SetValue( settingName.Substring( dot + 1 ), val );
124				}
125			}
126		}
127
128		/// <summary>
129		/// Make a new child storage under this one
130		/// </summary>
131		/// <param name="storageName">Name of the child storage to make</param>
132		/// <returns>New storage</returns>
133		public ISettingsStorage MakeChildStorage( string storageName )
134		{
135			return new RegistrySettingsStorage( storageKey.CreateSubKey( storageName ) );
136		}
137
138		/// <summary>
139		/// LoadSettings does nothing in this implementation, since the
140		/// registry is accessed directly.
141		/// </summary>
142		public void LoadSettings()
143		{
144		}
145
146		/// <summary>
147		/// SaveSettings does nothing in this implementation, since the
148		/// registry is accessed directly.
149		/// </summary>
150		public void SaveSettings()
151		{
152		}
153		#endregion
154
155		#region IDisposable Members
156		/// <summary>
157		/// Dispose of this object by closing the storage key, if any
158		/// </summary>
159		public void Dispose()
160		{
161			if ( storageKey != null )
162				storageKey.Close();
163		}
164
165		#endregion
166	}
167}