3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 #if FEATURE_WIN32_REGISTRY
8 namespace Microsoft.Win32 {
10 using System.Runtime.InteropServices;
11 using System.Runtime.Versioning;
16 * Registry encapsulation. Contains members representing all top level system
19 * @security(checkClassLinking=on)
21 //This class contains only static members and does not need to be serializable.
23 public static class Registry {
24 [System.Security.SecuritySafeCritical] // auto-generated
32 * This key should be used as the root for all user specific settings.
34 [ResourceExposure(ResourceScope.Machine)]
35 public static readonly RegistryKey CurrentUser = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_USER);
40 * This key should be used as the root for all machine specific settings.
42 [ResourceExposure(ResourceScope.Machine)]
43 public static readonly RegistryKey LocalMachine = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE);
48 * This is the root key of class information.
50 [ResourceExposure(ResourceScope.Machine)]
51 public static readonly RegistryKey ClassesRoot = RegistryKey.GetBaseKey(RegistryKey.HKEY_CLASSES_ROOT);
56 * This is the root of users.
58 [ResourceExposure(ResourceScope.Machine)]
59 public static readonly RegistryKey Users = RegistryKey.GetBaseKey(RegistryKey.HKEY_USERS);
62 * Performance Root Key.
64 * This is where dynamic performance data is stored on NT.
66 [ResourceExposure(ResourceScope.Machine)]
67 public static readonly RegistryKey PerformanceData = RegistryKey.GetBaseKey(RegistryKey.HKEY_PERFORMANCE_DATA);
70 * Current Config Root Key.
72 * This is where current configuration information is stored.
74 [ResourceExposure(ResourceScope.Machine)]
75 public static readonly RegistryKey CurrentConfig = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_CONFIG);
78 * Dynamic Data Root Key.
80 * LEGACY: This is where dynamic performance data is stored on Win9X.
81 * This does not exist on NT.
83 [ResourceExposure(ResourceScope.Machine)]
84 [Obsolete("The DynData registry key only works on Win9x, which is no longer supported by the CLR. On NT-based operating systems, use the PerformanceData registry key instead.")]
85 public static readonly RegistryKey DynData = RegistryKey.GetBaseKey(RegistryKey.HKEY_DYN_DATA);
88 // Following function will parse a keyName and returns the basekey for it.
89 // It will also store the subkey name in the out parameter.
90 // If the keyName is not valid, we will throw ArgumentException.
91 // The return value shouldn't be null.
93 [System.Security.SecurityCritical] // auto-generated
94 private static RegistryKey GetBaseKeyFromKeyName(string keyName, out string subKeyName) {
95 if( keyName == null) {
96 throw new ArgumentNullException("keyName");
100 int i = keyName.IndexOf('\\');
102 basekeyName = keyName.Substring(0, i).ToUpper(System.Globalization.CultureInfo.InvariantCulture);
105 basekeyName = keyName.ToUpper(System.Globalization.CultureInfo.InvariantCulture);
107 RegistryKey basekey = null;
109 switch(basekeyName) {
110 case "HKEY_CURRENT_USER":
111 basekey = Registry.CurrentUser;
113 case "HKEY_LOCAL_MACHINE":
114 basekey = Registry.LocalMachine;
116 case "HKEY_CLASSES_ROOT":
117 basekey = Registry.ClassesRoot;
120 basekey = Registry.Users;
122 case "HKEY_PERFORMANCE_DATA":
123 basekey = Registry.PerformanceData;
125 case "HKEY_CURRENT_CONFIG":
126 basekey = Registry.CurrentConfig;
128 case "HKEY_DYN_DATA":
129 basekey = RegistryKey.GetBaseKey(RegistryKey.HKEY_DYN_DATA);
132 throw new ArgumentException(Environment.GetResourceString("Arg_RegInvalidKeyName", "keyName"));
134 if( i == -1 || i == keyName.Length) {
135 subKeyName = string.Empty;
138 subKeyName = keyName.Substring(i + 1, keyName.Length - i - 1);
143 [System.Security.SecuritySafeCritical] // auto-generated
144 [ResourceExposure(ResourceScope.Machine)]
145 [ResourceConsumption(ResourceScope.Machine)]
146 public static object GetValue(string keyName, string valueName, object defaultValue ) {
148 RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
149 BCLDebug.Assert(basekey != null, "basekey can't be null.");
150 RegistryKey key = basekey.OpenSubKey(subKeyName);
151 if(key == null) { // if the key doesn't exist, do nothing
155 return key.GetValue(valueName, defaultValue);
162 [ResourceExposure(ResourceScope.Machine)]
163 [ResourceConsumption(ResourceScope.Machine)]
164 public static void SetValue(string keyName, string valueName, object value ) {
165 SetValue(keyName, valueName, value, RegistryValueKind.Unknown);
168 [System.Security.SecuritySafeCritical] // auto-generated
169 [ResourceExposure(ResourceScope.Machine)]
170 [ResourceConsumption(ResourceScope.Machine)]
171 public static void SetValue(string keyName, string valueName, object value, RegistryValueKind valueKind ) {
173 RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
174 BCLDebug.Assert(basekey != null, "basekey can't be null!");
175 RegistryKey key = basekey.CreateSubKey(subKeyName);
176 BCLDebug.Assert(key != null, "An exception should be thrown if failed!");
178 key.SetValue(valueName, value, valueKind);
186 #endif // !FEATURE_PAL
188 #endif // FEATURE_WIN32_REGISTRY