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/?p=license&r=2.4
5 // ****************************************************************
8 using System
.Diagnostics
;
14 /// Implementation of SettingsStorage for NUnit user settings,
15 /// based on storage of settings in the registry.
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.
23 public class RegistrySettingsStorage
: ISettingsStorage
25 #region Instance Variables
28 /// If not null, the registry key for this storage
30 private RegistryKey storageKey
;
34 #region Construction and Disposal
37 /// Construct a storage on top of a pre-created registry key
39 /// <param name="storageKey"></param>
40 public RegistrySettingsStorage( RegistryKey storageKey
)
42 this.storageKey
= storageKey
;
50 /// The registry key used to hold this storage
52 public RegistryKey StorageKey
54 get { return storageKey; }
59 #region ISettingsStorage Members
62 /// Load a setting from this storage
64 /// <param name="settingName">Name of the setting to load</param>
65 /// <returns>Value of the setting</returns>
66 public object GetSetting( string settingName
)
68 int dot
= settingName
.IndexOf( '.' );
70 return storageKey
.GetValue( settingName
);
72 using( RegistryKey subKey
= storageKey
.OpenSubKey( settingName
.Substring( 0, dot
) ) )
75 return subKey
.GetValue( settingName
.Substring( dot
+ 1 ) );
82 /// Remove a setting from the storage
84 /// <param name="settingName">Name of the setting to remove</param>
85 public void RemoveSetting( string settingName
)
87 int dot
= settingName
.IndexOf( '.' );
89 storageKey
.DeleteValue( settingName
, false );
92 using( RegistryKey subKey
= storageKey
.OpenSubKey( settingName
.Substring( 0, dot
), true ) )
95 subKey
.DeleteValue( settingName
.Substring( dot
+ 1 ) );
100 public void RemoveGroup( string groupName
)
102 storageKey
.DeleteSubKeyTree( groupName
);
106 /// Save a setting in this storage
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
)
112 object val
= settingValue
;
114 val
= ((bool)val
) ? 1 : 0;
116 int dot
= settingName
.IndexOf( '.' );
118 storageKey
.SetValue( settingName
, val
);
121 using( RegistryKey subKey
= storageKey
.CreateSubKey( settingName
.Substring( 0, dot
) ) )
123 subKey
.SetValue( settingName
.Substring( dot
+ 1 ), val
);
129 /// Make a new child storage under this one
131 /// <param name="storageName">Name of the child storage to make</param>
132 /// <returns>New storage</returns>
133 public ISettingsStorage
MakeChildStorage( string storageName
)
135 return new RegistrySettingsStorage( storageKey
.CreateSubKey( storageName
) );
139 /// LoadSettings does nothing in this implementation, since the
140 /// registry is accessed directly.
142 public void LoadSettings()
147 /// SaveSettings does nothing in this implementation, since the
148 /// registry is accessed directly.
150 public void SaveSettings()
155 #region IDisposable Members
157 /// Dispose of this object by closing the storage key, if any
159 public void Dispose()
161 if ( storageKey
!= null )