retag
[mcs.git] / nunit24 / ClientUtilities / util / RegistrySettingsStorage.cs
blob07f0e88b35398fde042ba4b06921de9e7cffebc4
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 // ****************************************************************
7 using System;
8 using System.Diagnostics;
9 using Microsoft.Win32;
11 namespace NUnit.Util
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
25 #region Instance Variables
27 /// <summary>
28 /// If not null, the registry key for this storage
29 /// </summary>
30 private RegistryKey storageKey;
32 #endregion
34 #region Construction and Disposal
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 )
42 this.storageKey = storageKey;
45 #endregion
47 #region Properties
49 /// <summary>
50 /// The registry key used to hold this storage
51 /// </summary>
52 public RegistryKey StorageKey
54 get { return storageKey; }
57 #endregion
59 #region ISettingsStorage Members
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 )
68 int dot = settingName.IndexOf( '.' );
69 if ( dot < 0 )
70 return storageKey.GetValue( settingName );
72 using( RegistryKey subKey = storageKey.OpenSubKey( settingName.Substring( 0, dot ) ) )
74 if ( subKey != null )
75 return subKey.GetValue( settingName.Substring( dot + 1 ) );
78 return null;
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 )
87 int dot = settingName.IndexOf( '.' );
88 if ( dot < 0 )
89 storageKey.DeleteValue( settingName, false );
90 else
92 using( RegistryKey subKey = storageKey.OpenSubKey( settingName.Substring( 0, dot ), true ) )
94 if ( subKey != null )
95 subKey.DeleteValue( settingName.Substring( dot + 1 ) );
100 public void RemoveGroup( string groupName )
102 storageKey.DeleteSubKeyTree( groupName );
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 )
112 object val = settingValue;
113 if ( val is bool )
114 val = ((bool)val) ? 1 : 0;
116 int dot = settingName.IndexOf( '.' );
117 if ( dot < 0 )
118 storageKey.SetValue( settingName, val );
119 else
121 using( RegistryKey subKey = storageKey.CreateSubKey( settingName.Substring( 0, dot ) ) )
123 subKey.SetValue( settingName.Substring( dot + 1 ), val );
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 )
135 return new RegistrySettingsStorage( storageKey.CreateSubKey( storageName ) );
138 /// <summary>
139 /// LoadSettings does nothing in this implementation, since the
140 /// registry is accessed directly.
141 /// </summary>
142 public void LoadSettings()
146 /// <summary>
147 /// SaveSettings does nothing in this implementation, since the
148 /// registry is accessed directly.
149 /// </summary>
150 public void SaveSettings()
153 #endregion
155 #region IDisposable Members
156 /// <summary>
157 /// Dispose of this object by closing the storage key, if any
158 /// </summary>
159 public void Dispose()
161 if ( storageKey != null )
162 storageKey.Close();
165 #endregion