File: RegistrySettingsStorage.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • 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 (167 lines) | stat: -rw-r--r-- 4,307 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// ****************************************************************
// Copyright 2002-2003, Charlie Poole
// This is free software licensed under the NUnit license. You may
// obtain a copy of the license at http://nunit.org/?p=license&r=2.4
// ****************************************************************

using System;
using System.Diagnostics;
using Microsoft.Win32;

namespace NUnit.Util
{
	/// <summary>
	/// Implementation of SettingsStorage for NUnit user settings,
	/// based on storage of settings in the registry.
	/// 
	/// Setting names containing a dot are interpreted as a 
	/// reference to a subkey. Only the first dot is used
	/// in this way, since the feature is only intended
	/// to support legacy registry settings, which are not
	/// nested any deeper.
	/// </summary>
	public class RegistrySettingsStorage : ISettingsStorage
	{
		#region Instance Variables

		/// <summary>
		/// If not null, the registry key for this storage
		/// </summary>
		private RegistryKey storageKey;

		#endregion

		#region Construction and Disposal

		/// <summary>
		/// Construct a storage on top of a pre-created registry key
		/// </summary>
		/// <param name="storageKey"></param>
		public RegistrySettingsStorage( RegistryKey storageKey )
		{
			this.storageKey = storageKey;
		}

		#endregion

		#region Properties

		/// <summary>
		/// The registry key used to hold this storage
		/// </summary>
		public RegistryKey StorageKey
		{
			get { return storageKey; }
		}

		#endregion

		#region ISettingsStorage Members

		/// <summary>
		/// Load a setting from this storage
		/// </summary>
		/// <param name="settingName">Name of the setting to load</param>
		/// <returns>Value of the setting</returns>
		public object GetSetting( string settingName )
		{
			int dot = settingName.IndexOf( '.' );
			if ( dot < 0 )
				return storageKey.GetValue( settingName );

			using( RegistryKey subKey = storageKey.OpenSubKey( settingName.Substring( 0, dot ) ) )
			{
				if ( subKey != null )
					return subKey.GetValue( settingName.Substring( dot + 1 ) );
			}

			return null;
		}

		/// <summary>
		/// Remove a setting from the storage
		/// </summary>
		/// <param name="settingName">Name of the setting to remove</param>
		public void RemoveSetting( string settingName )
		{
			int dot = settingName.IndexOf( '.' );
			if ( dot < 0 )
				storageKey.DeleteValue( settingName, false );
			else
			{
				using( RegistryKey subKey = storageKey.OpenSubKey( settingName.Substring( 0, dot ), true ) )
				{
					if ( subKey != null )
						subKey.DeleteValue( settingName.Substring( dot + 1 ) );
				}
			}
		}

		public void RemoveGroup( string groupName )
		{
			storageKey.DeleteSubKeyTree( groupName );
		}

		/// <summary>
		/// Save a setting in this storage
		/// </summary>
		/// <param name="settingName">Name of the setting to save</param>
		/// <param name="settingValue">Value to be saved</param>
		public void SaveSetting( string settingName, object settingValue )
		{
			object val = settingValue;
			if ( val is bool )
				val = ((bool)val) ? 1 : 0;

			int dot = settingName.IndexOf( '.' );
			if ( dot < 0 )
				storageKey.SetValue( settingName, val );
			else
			{
				using(  RegistryKey subKey = storageKey.CreateSubKey( settingName.Substring( 0, dot ) ) )
				{
					subKey.SetValue( settingName.Substring( dot + 1 ), val );
				}
			}
		}

		/// <summary>
		/// Make a new child storage under this one
		/// </summary>
		/// <param name="storageName">Name of the child storage to make</param>
		/// <returns>New storage</returns>
		public ISettingsStorage MakeChildStorage( string storageName )
		{
			return new RegistrySettingsStorage( storageKey.CreateSubKey( storageName ) );
		}

		/// <summary>
		/// LoadSettings does nothing in this implementation, since the
		/// registry is accessed directly.
		/// </summary>
		public void LoadSettings()
		{
		}

		/// <summary>
		/// SaveSettings does nothing in this implementation, since the
		/// registry is accessed directly.
		/// </summary>
		public void SaveSettings()
		{
		}
		#endregion

		#region IDisposable Members
		/// <summary>
		/// Dispose of this object by closing the storage key, if any
		/// </summary>
		public void Dispose()
		{
			if ( storageKey != null )
				storageKey.Close();
		}

		#endregion
	}
}