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
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 '***********************************************************************************/
31 using System
.Diagnostics
;
32 using Microsoft
.Win32
;
37 /// Implementation of SettingsStorage for NUnit user settings,
38 /// based on storage of settings in the registry.
40 public class RegistrySettingsStorage
: SettingsStorage
, IDisposable
42 #region Instance Variables
45 /// If not null, the registry key for this storage
47 private RegistryKey storageKey
;
51 #region Construction and Disposal
54 /// Construct a storage as a child of another storage
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
);
65 /// Construct a storage using a registry key. This constructor is
66 /// intended for use at the top level of the hierarchy.
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
);
77 /// Construct a storage on top of a given key, using the key's name
79 /// <param name="storageKey"></param>
80 public RegistrySettingsStorage( RegistryKey storageKey
)
81 : base( storageKey
.Name
, null )
83 this.storageKey
= storageKey
;
87 /// Dispose of this object by closing the storage key, if any
89 public override void Dispose()
91 if ( storageKey
!= null )
100 /// The registry key used to hold this storage
102 public RegistryKey StorageKey
104 get { return storageKey; }
108 /// The count of settings in this storage
110 public override int SettingsCount
112 get { return storageKey.ValueCount; }
120 /// Find out if a child storage exists
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
) )
133 /// Make a new child storage under this one
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 );
143 /// Load a setting from this storage
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
);
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.
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 );
167 /// Load a string setting from this storage
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();
181 /// Load a setting from this storage or return a default value
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
);
192 /// Load an integer setting from this storage or return a default value
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
);
207 /// Load a string setting from this storage or return a default value
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();
222 /// Remove a setting from the storage
224 /// <param name="settingName">Name of the setting to remove</param>
225 public override void RemoveSetting( string settingName
)
227 storageKey
.DeleteValue( settingName
, false );
231 /// Save a setting in this storage
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
);
241 /// Static function that clears out the contents of a key
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
);
254 /// Clear all settings from the storage - empty storage remains
256 public override void Clear()
258 ClearKey( storageKey
);