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/?p=license&r=2.4. 5 // **************************************************************** 6 7 namespace NUnit.Util 8 { 9 using System; 10 using System.IO; 11 using System.Text; 12 using Microsoft.Win32; 13 14 /// <summary> 15 /// NUnitRegistry provides static properties for NUnit's 16 /// CurrentUser and LocalMachine subkeys. 17 /// </summary> 18 public class NUnitRegistry 19 { 20 public static readonly string KEY = 21 @"Software\nunit.org\Nunit\2.4"; 22 23 public static readonly string LEGACY_KEY = 24 @"Software\Nascent Software\Nunit\"; 25 26 private static bool testMode = false; 27 public static readonly string TEST_KEY = 28 @"Software\nunit.org\Nunit-Test"; 29 30 31 /// <summary> 32 /// Prevent construction of object 33 /// </summary> NUnitRegistry()34 private NUnitRegistry() { } 35 36 public static bool TestMode 37 { 38 get { return testMode; } 39 set { testMode = value; } 40 } 41 42 /// <summary> 43 /// Registry subkey for the current user 44 /// </summary> 45 public static RegistryKey CurrentUser 46 { 47 get 48 { 49 if ( testMode ) 50 return Registry.CurrentUser.CreateSubKey( TEST_KEY ); 51 52 RegistryKey newKey = Registry.CurrentUser.OpenSubKey( KEY, true ); 53 if (newKey == null) 54 { 55 newKey = Registry.CurrentUser.CreateSubKey( KEY ); 56 RegistryKey oldKey = Registry.CurrentUser.OpenSubKey( LEGACY_KEY ); 57 if ( oldKey != null ) 58 { 59 CopyKey( oldKey, newKey ); 60 oldKey.Close(); 61 } 62 } 63 64 return newKey; 65 } 66 } 67 KeyExists( string subkey )68 public static bool KeyExists( string subkey ) 69 { 70 using ( RegistryKey key = Registry.CurrentUser.OpenSubKey( subkey, true ) ) 71 { 72 return key != null; 73 } 74 } 75 76 /// <summary> 77 /// Registry subkey for the local machine 78 /// </summary> 79 public static RegistryKey LocalMachine 80 { 81 get { return Registry.LocalMachine.CreateSubKey( testMode ? TEST_KEY : KEY ); } 82 } 83 ClearTestKeys()84 public static void ClearTestKeys() 85 { 86 ClearSubKey( Registry.CurrentUser, TEST_KEY ); 87 //ClearSubKey( Registry.LocalMachine, TEST_KEY ); 88 } 89 90 /// <summary> 91 /// Static helper method that clears out the contents of a subkey 92 /// </summary> 93 /// <param name="baseKey">Base key for the subkey</param> 94 /// <param name="subKey">Name of the subkey</param> ClearSubKey( RegistryKey baseKey, string subKey )95 private static void ClearSubKey( RegistryKey baseKey, string subKey ) 96 { 97 using( RegistryKey key = baseKey.OpenSubKey( subKey, true ) ) 98 { 99 if ( key != null ) ClearKey( key ); 100 } 101 } 102 103 /// <summary> 104 /// Static function that clears out the contents of a key 105 /// </summary> 106 /// <param name="key">Key to be cleared</param> ClearKey( RegistryKey key )107 public static void ClearKey( RegistryKey key ) 108 { 109 foreach( string name in key.GetValueNames() ) 110 key.DeleteValue( name ); 111 112 // TODO: This throws under Mono - Restore when bug is fixed 113 //foreach( string name in key.GetSubKeyNames() ) 114 // key.DeleteSubKeyTree( name ); 115 116 foreach (string name in key.GetSubKeyNames()) 117 { 118 ClearSubKey(key, name); 119 key.DeleteSubKey( name ); 120 } 121 } 122 123 /// <summary> 124 /// Static method that copies the contents of one key to another 125 /// </summary> 126 /// <param name="fromKey">The source key for the copy</param> 127 /// <param name="toKey">The target key for the copy</param> CopyKey( RegistryKey fromKey, RegistryKey toKey )128 public static void CopyKey( RegistryKey fromKey, RegistryKey toKey ) 129 { 130 foreach( string name in fromKey.GetValueNames() ) 131 toKey.SetValue( name, fromKey.GetValue( name ) ); 132 133 foreach( string name in fromKey.GetSubKeyNames() ) 134 using( RegistryKey fromSubKey = fromKey.OpenSubKey( name ) ) 135 using( RegistryKey toSubKey = toKey.CreateSubKey( name ) ) 136 { 137 CopyKey( fromSubKey, toSubKey ); 138 } 139 } 140 } 141 } 142