PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/util/NUnitRegistry.cs

#
C# | 133 lines | 82 code | 18 blank | 33 comment | 8 complexity | 556364f63416a0af571fdb7aee90db9c MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You
  3. // may obtain a copy of the license as well as information regarding
  4. // copyright ownership at http://nunit.org.
  5. // ****************************************************************
  6. namespace NUnit.Util
  7. {
  8. using System;
  9. using System.IO;
  10. using System.Text;
  11. using Microsoft.Win32;
  12. /// <summary>
  13. /// NUnitRegistry provides static properties for NUnit's
  14. /// subkey under CurrentUser.
  15. /// </summary>
  16. public class NUnitRegistry
  17. {
  18. public static readonly string KEY =
  19. @"Software\nunit.org\Nunit\2.4";
  20. public static readonly string LEGACY_KEY =
  21. @"Software\Nascent Software\Nunit\";
  22. private static bool testMode = false;
  23. public static readonly string TEST_KEY =
  24. @"Software\nunit.org\Nunit-Test";
  25. /// <summary>
  26. /// Prevent construction of object
  27. /// </summary>
  28. private NUnitRegistry() { }
  29. public static bool TestMode
  30. {
  31. get { return testMode; }
  32. set { testMode = value; }
  33. }
  34. /// <summary>
  35. /// Registry subkey for the current user
  36. /// </summary>
  37. public static RegistryKey CurrentUser
  38. {
  39. get
  40. {
  41. if ( testMode )
  42. return Registry.CurrentUser.CreateSubKey( TEST_KEY );
  43. RegistryKey newKey = Registry.CurrentUser.OpenSubKey( KEY, true );
  44. if (newKey == null)
  45. {
  46. newKey = Registry.CurrentUser.CreateSubKey( KEY );
  47. RegistryKey oldKey = Registry.CurrentUser.OpenSubKey( LEGACY_KEY );
  48. if ( oldKey != null )
  49. {
  50. CopyKey( oldKey, newKey );
  51. oldKey.Close();
  52. }
  53. }
  54. return newKey;
  55. }
  56. }
  57. public static bool KeyExists( string subkey )
  58. {
  59. using ( RegistryKey key = Registry.CurrentUser.OpenSubKey( subkey, true ) )
  60. {
  61. return key != null;
  62. }
  63. }
  64. public static void ClearTestKeys()
  65. {
  66. ClearSubKey( Registry.CurrentUser, TEST_KEY );
  67. //ClearSubKey( Registry.LocalMachine, TEST_KEY );
  68. }
  69. /// <summary>
  70. /// Static helper method that clears out the contents of a subkey
  71. /// </summary>
  72. /// <param name="baseKey">Base key for the subkey</param>
  73. /// <param name="subKey">Name of the subkey</param>
  74. private static void ClearSubKey( RegistryKey baseKey, string subKey )
  75. {
  76. using( RegistryKey key = baseKey.OpenSubKey( subKey, true ) )
  77. {
  78. if ( key != null ) ClearKey( key );
  79. }
  80. }
  81. /// <summary>
  82. /// Static function that clears out the contents of a key
  83. /// </summary>
  84. /// <param name="key">Key to be cleared</param>
  85. public static void ClearKey( RegistryKey key )
  86. {
  87. foreach( string name in key.GetValueNames() )
  88. key.DeleteValue( name );
  89. // TODO: This throws under Mono - Restore when bug is fixed
  90. //foreach( string name in key.GetSubKeyNames() )
  91. // key.DeleteSubKeyTree( name );
  92. foreach (string name in key.GetSubKeyNames())
  93. {
  94. ClearSubKey(key, name);
  95. key.DeleteSubKey( name );
  96. }
  97. }
  98. /// <summary>
  99. /// Static method that copies the contents of one key to another
  100. /// </summary>
  101. /// <param name="fromKey">The source key for the copy</param>
  102. /// <param name="toKey">The target key for the copy</param>
  103. public static void CopyKey( RegistryKey fromKey, RegistryKey toKey )
  104. {
  105. foreach( string name in fromKey.GetValueNames() )
  106. toKey.SetValue( name, fromKey.GetValue( name ) );
  107. foreach( string name in fromKey.GetSubKeyNames() )
  108. using( RegistryKey fromSubKey = fromKey.OpenSubKey( name ) )
  109. using( RegistryKey toSubKey = toKey.CreateSubKey( name ) )
  110. {
  111. CopyKey( fromSubKey, toSubKey );
  112. }
  113. }
  114. }
  115. }