File: NUnitRegistry.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (141 lines) | stat: -rw-r--r-- 3,801 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// ****************************************************************
// 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;

	/// <summary>
	/// NUnitRegistry provides static properties for NUnit's
	/// CurrentUser and LocalMachine subkeys.
	/// </summary>
	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";


		/// <summary>
		/// Prevent construction of object
		/// </summary>
		private NUnitRegistry() { }

		public static bool TestMode
		{
			get { return testMode; }
			set { testMode = value; }
		}

		/// <summary>
		/// Registry subkey for the current user
		/// </summary>
		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;
			} 
		}

		/// <summary>
		/// Registry subkey for the local machine
		/// </summary>
		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 );	
		}

		/// <summary>
		/// Static helper method that clears out the contents of a subkey
		/// </summary>
		/// <param name="baseKey">Base key for the subkey</param>
		/// <param name="subKey">Name of the subkey</param>
		private static void ClearSubKey( RegistryKey baseKey, string subKey )
		{
			using( RegistryKey key = baseKey.OpenSubKey( subKey, true ) )
			{
				if ( key != null ) ClearKey( key );
			}
		}

		/// <summary>
		/// Static function that clears out the contents of a key
		/// </summary>
		/// <param name="key">Key to be cleared</param>
		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 );
			}
		}

		/// <summary>
		/// Static method that copies the contents of one key to another
		/// </summary>
		/// <param name="fromKey">The source key for the copy</param>
		/// <param name="toKey">The target key for the copy</param>
		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 );
				}
		}
	}
}