// **************************************************************** // This is free software licensed under the NUnit license. You // may obtain a copy of the license as well as information regarding // copyright ownership at http://nunit.org/?p=license&r=2.4. // **************************************************************** namespace NUnit.Util { using System; using System.IO; using System.Text; using Microsoft.Win32; /// /// NUnitRegistry provides static properties for NUnit's /// CurrentUser and LocalMachine subkeys. /// public class NUnitRegistry { public static readonly string KEY = @"Software\nunit.org\Nunit\2.4"; public static readonly string LEGACY_KEY = @"Software\Nascent Software\Nunit\"; private static bool testMode = false; public static readonly string TEST_KEY = @"Software\nunit.org\Nunit-Test"; /// /// Prevent construction of object /// private NUnitRegistry() { } public static bool TestMode { get { return testMode; } set { testMode = value; } } /// /// Registry subkey for the current user /// public static RegistryKey CurrentUser { get { if ( testMode ) return Registry.CurrentUser.CreateSubKey( TEST_KEY ); RegistryKey newKey = Registry.CurrentUser.OpenSubKey( KEY, true ); if (newKey == null) { newKey = Registry.CurrentUser.CreateSubKey( KEY ); RegistryKey oldKey = Registry.CurrentUser.OpenSubKey( LEGACY_KEY ); if ( oldKey != null ) { CopyKey( oldKey, newKey ); oldKey.Close(); } } return newKey; } } public static bool KeyExists( string subkey ) { using ( RegistryKey key = Registry.CurrentUser.OpenSubKey( subkey, true ) ) { return key != null; } } /// /// Registry subkey for the local machine /// public static RegistryKey LocalMachine { get { return Registry.LocalMachine.CreateSubKey( testMode ? TEST_KEY : KEY ); } } public static void ClearTestKeys() { ClearSubKey( Registry.CurrentUser, TEST_KEY ); //ClearSubKey( Registry.LocalMachine, TEST_KEY ); } /// /// Static helper method that clears out the contents of a subkey /// /// Base key for the subkey /// Name of the subkey private static void ClearSubKey( RegistryKey baseKey, string subKey ) { using( RegistryKey key = baseKey.OpenSubKey( subKey, true ) ) { if ( key != null ) ClearKey( key ); } } /// /// Static function that clears out the contents of a key /// /// Key to be cleared public static void ClearKey( RegistryKey key ) { foreach( string name in key.GetValueNames() ) key.DeleteValue( name ); // TODO: This throws under Mono - Restore when bug is fixed //foreach( string name in key.GetSubKeyNames() ) // key.DeleteSubKeyTree( name ); foreach (string name in key.GetSubKeyNames()) { ClearSubKey(key, name); key.DeleteSubKey( name ); } } /// /// Static method that copies the contents of one key to another /// /// The source key for the copy /// The target key for the copy public static void CopyKey( RegistryKey fromKey, RegistryKey toKey ) { foreach( string name in fromKey.GetValueNames() ) toKey.SetValue( name, fromKey.GetValue( name ) ); foreach( string name in fromKey.GetSubKeyNames() ) using( RegistryKey fromSubKey = fromKey.OpenSubKey( name ) ) using( RegistryKey toSubKey = toKey.CreateSubKey( name ) ) { CopyKey( fromSubKey, toSubKey ); } } } }