**** Merged from MCS ****
[mono-project.git] / mcs / nunit20 / util / RegistrySettingsStorage.cs
bloba07c389e02d91f61dc858eb902f8d320dc64351d
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
4 ' Copyright 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright 2000-2002 Philip A. Craig
7 ' This software is provided 'as-is', without any express or implied warranty. In no
8 ' event will the authors be held liable for any damages arising from the use of this
9 ' software.
11 ' Permission is granted to anyone to use this software for any purpose, including
12 ' commercial applications, and to alter it and redistribute it freely, subject to the
13 ' following restrictions:
15 ' 1. The origin of this software must not be misrepresented; you must not claim that
16 ' you wrote the original software. If you use this software in a product, an
17 ' acknowledgment (see the following) in the product documentation is required.
19 ' Portions Copyright 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright 2000-2002 Philip A. Craig
22 ' 2. Altered source versions must be plainly marked as such, and must not be
23 ' misrepresented as being the original software.
25 ' 3. This notice may not be removed or altered from any source distribution.
27 '***********************************************************************************/
28 #endregion
30 using System;
31 using System.Diagnostics;
32 using Microsoft.Win32;
34 namespace NUnit.Util
36 /// <summary>
37 /// Implementation of SettingsStorage for NUnit user settings,
38 /// based on storage of settings in the registry.
39 /// </summary>
40 public class RegistrySettingsStorage : SettingsStorage, IDisposable
42 #region Instance Variables
44 /// <summary>
45 /// If not null, the registry key for this storage
46 /// </summary>
47 private RegistryKey storageKey;
49 #endregion
51 #region Construction and Disposal
53 /// <summary>
54 /// Construct a storage as a child of another storage
55 /// </summary>
56 /// <param name="storageName">The name to give the storage</param>
57 /// <param name="parentStorage">The parent in which the storage is to be created</param>
58 public RegistrySettingsStorage( string storageName, RegistrySettingsStorage parentStorage )
59 : base( storageName, parentStorage )
61 this.storageKey = parentStorage.StorageKey.CreateSubKey( storageName );
64 /// <summary>
65 /// Construct a storage using a registry key. This constructor is
66 /// intended for use at the top level of the hierarchy.
67 /// </summary>
68 /// <param name="storageName">The name to give the storage</param>
69 /// <param name="parentKey">The registry Key under which the storage will be created</param>
70 public RegistrySettingsStorage( string storageName, RegistryKey parentKey )
71 : base ( storageName, null )
73 this.storageKey = parentKey.CreateSubKey( storageName );
76 /// <summary>
77 /// Construct a storage on top of a given key, using the key's name
78 /// </summary>
79 /// <param name="storageKey"></param>
80 public RegistrySettingsStorage( RegistryKey storageKey )
81 : base( storageKey.Name, null )
83 this.storageKey = storageKey;
86 /// <summary>
87 /// Dispose of this object by closing the storage key, if any
88 /// </summary>
89 public override void Dispose()
91 if ( storageKey != null )
92 storageKey.Close();
95 #endregion
97 #region Properties
99 /// <summary>
100 /// The registry key used to hold this storage
101 /// </summary>
102 public RegistryKey StorageKey
104 get { return storageKey; }
107 /// <summary>
108 /// The count of settings in this storage
109 /// </summary>
110 public override int SettingsCount
112 get { return storageKey.ValueCount; }
115 #endregion
117 #region Methods
119 /// <summary>
120 /// Find out if a child storage exists
121 /// </summary>
122 /// <param name="storageName">Name of the child storage</param>
123 /// <returns>True if the child storage exists</returns>
124 public override bool ChildStorageExists( string storageName )
126 using (RegistryKey key = storageKey.OpenSubKey( storageName ) )
128 return key != null;
132 /// <summary>
133 /// Make a new child storage under this one
134 /// </summary>
135 /// <param name="storageName">Name of the child storage to make</param>
136 /// <returns>New storage</returns>
137 public override SettingsStorage MakeChildStorage( string storageName )
139 return new RegistrySettingsStorage( storageName, this );
142 /// <summary>
143 /// Load a setting from this storage
144 /// </summary>
145 /// <param name="settingName">Name of the setting to load</param>
146 /// <returns>Value of the setting</returns>
147 public override object LoadSetting( string settingName )
149 return storageKey.GetValue( settingName );
152 /// <summary>
153 /// Load an int setting from this storage. Since int is a
154 /// value type, we can't return null so zero is used to
155 /// indicate that nothing was found - or the found value
156 /// was zero. If you need to distinguish, use your own
157 /// default value or call LoadSetting and check for null.
158 /// </summary>
159 /// <param name="settingName">Name of the setting to load</param>
160 /// <returns>Value of the setting or zero if missing</returns>
161 public override int LoadIntSetting( string settingName )
163 return LoadIntSetting( settingName, 0 );
166 /// <summary>
167 /// Load a string setting from this storage
168 /// </summary>
169 /// <param name="settingName">Name of the setting to load</param>
170 /// <returns>Value of the setting</returns>
171 public override string LoadStringSetting( string settingName )
173 object resultValue = storageKey.GetValue( settingName );
174 if ( resultValue == null || resultValue is string )
175 return (string) resultValue;
177 return resultValue.ToString();
180 /// <summary>
181 /// Load a setting from this storage or return a default value
182 /// </summary>
183 /// <param name="settingName">Name of setting to load</param>
184 /// <param name="defaultValue">Value to return if the seeting is not present</param>
185 /// <returns>Value of the setting or the default</returns>
186 public override object LoadSetting( string settingName, object defaultValue )
188 return storageKey.GetValue( settingName, defaultValue );
191 /// <summary>
192 /// Load an integer setting from this storage or return a default value
193 /// </summary>
194 /// <param name="settingName">Name of setting to load</param>
195 /// <param name="defaultValue">Value to return if the seeting is not present</param>
196 /// <returns>Value of the setting or the default</returns>
197 public override int LoadIntSetting( string settingName, int defaultValue )
199 object resultValue = storageKey.GetValue( settingName, defaultValue );
200 if ( resultValue is int )
201 return (int)resultValue;
203 return int.Parse( (string)resultValue );
206 /// <summary>
207 /// Load a string setting from this storage or return a default value
208 /// </summary>
209 /// <param name="settingName">Name of setting to load</param>
210 /// <param name="defaultValue">Value to return if the seeting is not present</param>
211 /// <returns>Value of the setting or the default</returns>
212 public override string LoadStringSetting( string settingName, string defaultValue )
214 object resultValue = storageKey.GetValue( settingName, defaultValue );
215 if ( resultValue is string )
216 return (string) resultValue;
218 return resultValue.ToString();
221 /// <summary>
222 /// Remove a setting from the storage
223 /// </summary>
224 /// <param name="settingName">Name of the setting to remove</param>
225 public override void RemoveSetting( string settingName )
227 storageKey.DeleteValue( settingName, false );
230 /// <summary>
231 /// Save a setting in this storage
232 /// </summary>
233 /// <param name="settingName">Name of the setting to save</param>
234 /// <param name="settingValue">Value to be saved</param>
235 public override void SaveSetting( string settingName, object settingValue )
237 storageKey.SetValue( settingName, settingValue );
240 /// <summary>
241 /// Static function that clears out the contents of a key
242 /// </summary>
243 /// <param name="key">Key to be cleared</param>
244 public static void ClearKey( RegistryKey key )
246 foreach( string name in key.GetValueNames() )
247 key.DeleteValue( name );
249 foreach( string name in key.GetSubKeyNames() )
250 key.DeleteSubKeyTree( name );
253 /// <summary>
254 /// Clear all settings from the storage - empty storage remains
255 /// </summary>
256 public override void Clear()
258 ClearKey( storageKey );
261 #endregion