PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/util/RegistrySettingsStorage.cs

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