|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Runtime.Versioning; |
| 9 | + |
5 | 10 | namespace Microsoft.Win32
|
6 | 11 | {
|
| 12 | + /** |
| 13 | + * Registry encapsulation. Contains members representing all top level system |
| 14 | + * keys. |
| 15 | + * |
| 16 | + * @security(checkClassLinking=on) |
| 17 | + */ |
| 18 | + //This class contains only static members and does not need to be serializable. |
7 | 19 | internal static class Registry
|
8 | 20 | {
|
9 |
| - public static readonly RegistryKey CurrentUser = RegistryKey.CurrentUser; |
10 |
| - public static readonly RegistryKey LocalMachine = RegistryKey.LocalMachine; |
| 21 | + /** |
| 22 | + * Current User Key. |
| 23 | + * |
| 24 | + * This key should be used as the root for all user specific settings. |
| 25 | + */ |
| 26 | + public static readonly RegistryKey CurrentUser = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_USER); |
| 27 | + |
| 28 | + /** |
| 29 | + * Local Machine Key. |
| 30 | + * |
| 31 | + * This key should be used as the root for all machine specific settings. |
| 32 | + */ |
| 33 | + public static readonly RegistryKey LocalMachine = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE); |
| 34 | + |
| 35 | + /** |
| 36 | + * Classes Root Key. |
| 37 | + * |
| 38 | + * This is the root key of class information. |
| 39 | + */ |
| 40 | + public static readonly RegistryKey ClassesRoot = RegistryKey.GetBaseKey(RegistryKey.HKEY_CLASSES_ROOT); |
| 41 | + |
| 42 | + /** |
| 43 | + * Users Root Key. |
| 44 | + * |
| 45 | + * This is the root of users. |
| 46 | + */ |
| 47 | + public static readonly RegistryKey Users = RegistryKey.GetBaseKey(RegistryKey.HKEY_USERS); |
| 48 | + |
| 49 | + /** |
| 50 | + * Performance Root Key. |
| 51 | + * |
| 52 | + * This is where dynamic performance data is stored on NT. |
| 53 | + */ |
| 54 | + public static readonly RegistryKey PerformanceData = RegistryKey.GetBaseKey(RegistryKey.HKEY_PERFORMANCE_DATA); |
| 55 | + |
| 56 | + /** |
| 57 | + * Current Config Root Key. |
| 58 | + * |
| 59 | + * This is where current configuration information is stored. |
| 60 | + */ |
| 61 | + public static readonly RegistryKey CurrentConfig = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_CONFIG); |
| 62 | + |
| 63 | + // |
| 64 | + // Following function will parse a keyName and returns the basekey for it. |
| 65 | + // It will also store the subkey name in the out parameter. |
| 66 | + // If the keyName is not valid, we will throw ArgumentException. |
| 67 | + // The return value shouldn't be null. |
| 68 | + // |
| 69 | + private static RegistryKey GetBaseKeyFromKeyName(string keyName, out string subKeyName) |
| 70 | + { |
| 71 | + if (keyName == null) |
| 72 | + { |
| 73 | + throw new ArgumentNullException(nameof(keyName)); |
| 74 | + } |
| 75 | + |
| 76 | + string basekeyName; |
| 77 | + int i = keyName.IndexOf('\\'); |
| 78 | + if (i != -1) |
| 79 | + { |
| 80 | + basekeyName = keyName.Substring(0, i).ToUpper(System.Globalization.CultureInfo.InvariantCulture); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + basekeyName = keyName.ToUpper(System.Globalization.CultureInfo.InvariantCulture); |
| 85 | + } |
| 86 | + RegistryKey basekey = null; |
| 87 | + |
| 88 | + switch (basekeyName) |
| 89 | + { |
| 90 | + case "HKEY_CURRENT_USER": |
| 91 | + basekey = Registry.CurrentUser; |
| 92 | + break; |
| 93 | + case "HKEY_LOCAL_MACHINE": |
| 94 | + basekey = Registry.LocalMachine; |
| 95 | + break; |
| 96 | + case "HKEY_CLASSES_ROOT": |
| 97 | + basekey = Registry.ClassesRoot; |
| 98 | + break; |
| 99 | + case "HKEY_USERS": |
| 100 | + basekey = Registry.Users; |
| 101 | + break; |
| 102 | + case "HKEY_PERFORMANCE_DATA": |
| 103 | + basekey = Registry.PerformanceData; |
| 104 | + break; |
| 105 | + case "HKEY_CURRENT_CONFIG": |
| 106 | + basekey = Registry.CurrentConfig; |
| 107 | + break; |
| 108 | + default: |
| 109 | + throw new ArgumentException(SR.Format(SR.Arg_RegInvalidKeyName, nameof(keyName))); |
| 110 | + } |
| 111 | + if (i == -1 || i == keyName.Length) |
| 112 | + { |
| 113 | + subKeyName = string.Empty; |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + subKeyName = keyName.Substring(i + 1, keyName.Length - i - 1); |
| 118 | + } |
| 119 | + return basekey; |
| 120 | + } |
| 121 | + |
| 122 | + public static object GetValue(string keyName, string valueName, object defaultValue) |
| 123 | + { |
| 124 | + string subKeyName; |
| 125 | + RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName); |
| 126 | + BCLDebug.Assert(basekey != null, "basekey can't be null."); |
| 127 | + RegistryKey key = basekey.OpenSubKey(subKeyName); |
| 128 | + if (key == null) |
| 129 | + { // if the key doesn't exist, do nothing |
| 130 | + return null; |
| 131 | + } |
| 132 | + try |
| 133 | + { |
| 134 | + return key.GetValue(valueName, defaultValue); |
| 135 | + } |
| 136 | + finally |
| 137 | + { |
| 138 | + key.Close(); |
| 139 | + } |
| 140 | + } |
11 | 141 | }
|
12 | 142 | }
|
13 | 143 |
|
|
0 commit comments