2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / nunit20 / util / NUnitRegistry.cs
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
3 '
4 ' Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright  2000-2002 Philip A. Craig
6 '
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 
9 ' software.
10
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:
14 '
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.
18 '
19 ' Portions Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright  2000-2002 Philip A. Craig
21 '
22 ' 2. Altered source versions must be plainly marked as such, and must not be 
23 ' misrepresented as being the original software.
24 '
25 ' 3. This notice may not be removed or altered from any source distribution.
26 '
27 '***********************************************************************************/
28 #endregion
29
30 namespace NUnit.Util
31 {
32         using System;
33         using System.IO;
34         using System.Text;
35         using System.Windows.Forms;
36         using Microsoft.Win32;
37
38         /// <summary>
39         /// NUnitRegistry provides static properties for NUnit's
40         /// CurrentUser and LocalMachine subkeys.
41         /// </summary>
42         public class NUnitRegistry
43         {
44                 private static readonly string KEY = 
45                         @"Software\Nascent Software\Nunit\";
46
47                 private static bool testMode = false;
48                 private static string testKey = 
49                         @"Software\Nascent Software\Nunit-Test";
50
51
52                 /// <summary>
53                 /// Prevent construction of object
54                 /// </summary>
55                 private NUnitRegistry() { }
56
57                 public static bool TestMode
58                 {
59                         get { return testMode; }
60                         set { testMode = value; }
61                 }
62
63                 public static string TestKey
64                 {
65                         get { return testKey; }
66                         set { testKey = value; }
67                 }
68
69                 /// <summary>
70                 /// Registry subkey for the current user
71                 /// </summary>
72                 public static RegistryKey CurrentUser
73                 {
74                         get 
75                         {
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 )
80                                 //              create it
81                                 //              open old key
82                                 //              if ( it was opened )
83                                 //                      copy entries to new key
84                                 //      return new key
85                                 return Registry.CurrentUser.CreateSubKey( testMode ? testKey : KEY ); 
86                         }
87                 }
88
89                 /// <summary>
90                 /// Registry subkey for the local machine
91                 /// </summary>
92                 public static RegistryKey LocalMachine
93                 {
94                         get { return Registry.LocalMachine.CreateSubKey( testMode ? testKey : KEY ); }
95                 }
96
97                 public static void ClearTestKeys()
98                 {
99                         ClearSubKey( Registry.CurrentUser, testKey );
100                         ClearSubKey( Registry.LocalMachine, testKey );  
101                 }
102
103                 /// <summary>
104                 /// Static function that clears out the contents of a subkey
105                 /// </summary>
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 )
109                 {
110                         using( RegistryKey key = baseKey.OpenSubKey( subKey, true ) )
111                         {
112                                 if ( key != null ) ClearKey( key );
113                         }
114                 }
115
116                 /// <summary>
117                 /// Static function that clears out the contents of a key
118                 /// </summary>
119                 /// <param name="key">Key to be cleared</param>
120                 public static void ClearKey( RegistryKey key )
121                 {
122                         foreach( string name in key.GetValueNames() )
123                                 key.DeleteValue( name );
124
125                         foreach( string name in key.GetSubKeyNames() )
126                                 key.DeleteSubKeyTree( name );
127                 }
128         }
129 }