/src/NUnit/util/NUnitRegistry.cs
C# | 133 lines | 82 code | 18 blank | 33 comment | 8 complexity | 556364f63416a0af571fdb7aee90db9c MD5 | raw file
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 7namespace 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 /// subkey under CurrentUser. 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> 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 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 public static void ClearTestKeys() 77 { 78 ClearSubKey( Registry.CurrentUser, TEST_KEY ); 79 //ClearSubKey( Registry.LocalMachine, TEST_KEY ); 80 } 81 82 /// <summary> 83 /// Static helper method that clears out the contents of a subkey 84 /// </summary> 85 /// <param name="baseKey">Base key for the subkey</param> 86 /// <param name="subKey">Name of the subkey</param> 87 private static void ClearSubKey( RegistryKey baseKey, string subKey ) 88 { 89 using( RegistryKey key = baseKey.OpenSubKey( subKey, true ) ) 90 { 91 if ( key != null ) ClearKey( key ); 92 } 93 } 94 95 /// <summary> 96 /// Static function that clears out the contents of a key 97 /// </summary> 98 /// <param name="key">Key to be cleared</param> 99 public static void ClearKey( RegistryKey key ) 100 { 101 foreach( string name in key.GetValueNames() ) 102 key.DeleteValue( name ); 103 104 // TODO: This throws under Mono - Restore when bug is fixed 105 //foreach( string name in key.GetSubKeyNames() ) 106 // key.DeleteSubKeyTree( name ); 107 108 foreach (string name in key.GetSubKeyNames()) 109 { 110 ClearSubKey(key, name); 111 key.DeleteSubKey( name ); 112 } 113 } 114 115 /// <summary> 116 /// Static method that copies the contents of one key to another 117 /// </summary> 118 /// <param name="fromKey">The source key for the copy</param> 119 /// <param name="toKey">The target key for the copy</param> 120 public static void CopyKey( RegistryKey fromKey, RegistryKey toKey ) 121 { 122 foreach( string name in fromKey.GetValueNames() ) 123 toKey.SetValue( name, fromKey.GetValue( name ) ); 124 125 foreach( string name in fromKey.GetSubKeyNames() ) 126 using( RegistryKey fromSubKey = fromKey.OpenSubKey( name ) ) 127 using( RegistryKey toSubKey = toKey.CreateSubKey( name ) ) 128 { 129 CopyKey( fromSubKey, toSubKey ); 130 } 131 } 132 } 133}