1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
4 ' Copyright 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright 2000-2002 Philip A. Craig
7 ' This software is provided 'as-is', without any express or implied warranty. In no
8 ' event will the authors be held liable for any damages arising from the use of this
11 ' Permission is granted to anyone to use this software for any purpose, including
12 ' commercial applications, and to alter it and redistribute it freely, subject to the
13 ' following restrictions:
15 ' 1. The origin of this software must not be misrepresented; you must not claim that
16 ' you wrote the original software. If you use this software in a product, an
17 ' acknowledgment (see the following) in the product documentation is required.
19 ' Portions Copyright 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright 2000-2002 Philip A. Craig
22 ' 2. Altered source versions must be plainly marked as such, and must not be
23 ' misrepresented as being the original software.
25 ' 3. This notice may not be removed or altered from any source distribution.
27 '***********************************************************************************/
35 using System.Windows.Forms;
36 using Microsoft.Win32;
39 /// NUnitRegistry provides static properties for NUnit's
40 /// CurrentUser and LocalMachine subkeys.
42 public class NUnitRegistry
44 private static readonly string KEY =
45 @"Software\Nascent Software\Nunit\";
47 private static bool testMode = false;
48 private static string testKey =
49 @"Software\Nascent Software\Nunit-Test";
53 /// Prevent construction of object
55 private NUnitRegistry() { }
57 public static bool TestMode
59 get { return testMode; }
60 set { testMode = value; }
63 public static string TestKey
65 get { return testKey; }
66 set { testKey = value; }
70 /// Registry subkey for the current user
72 public static RegistryKey CurrentUser
76 // Todo: Code can go here to migrate the registry
77 // if we change our location.
78 // Try to open new key
79 // if ( key doesn't exist )
82 // if ( it was opened )
83 // copy entries to new key
85 return Registry.CurrentUser.CreateSubKey( testMode ? testKey : KEY );
90 /// Registry subkey for the local machine
92 public static RegistryKey LocalMachine
94 get { return Registry.LocalMachine.CreateSubKey( testMode ? testKey : KEY ); }
97 public static void ClearTestKeys()
99 ClearSubKey( Registry.CurrentUser, testKey );
100 ClearSubKey( Registry.LocalMachine, testKey );
104 /// Static function that clears out the contents of a subkey
106 /// <param name="baseKey">Base key for the subkey</param>
107 /// <param name="subKey">Name of the subkey</param>
108 public static void ClearSubKey( RegistryKey baseKey, string subKey )
110 using( RegistryKey key = baseKey.OpenSubKey( subKey, true ) )
112 if ( key != null ) ClearKey( key );
117 /// Static function that clears out the contents of a key
119 /// <param name="key">Key to be cleared</param>
120 public static void ClearKey( RegistryKey key )
122 foreach( string name in key.GetValueNames() )
123 key.DeleteValue( name );
125 foreach( string name in key.GetSubKeyNames() )
126 key.DeleteSubKeyTree( name );