summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/Microsoft/Win32/Registry.cs
blob: 3ee5f4648b35c31f3fd59e91ffd7ace822a6439f (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


namespace Microsoft.Win32 {
    using System;
    using System.Runtime.InteropServices;
    using System.Runtime.Versioning;

    /**
     * Registry encapsulation. Contains members representing all top level system
     * keys.
     *
     * @security(checkClassLinking=on)
     */
    //This class contains only static members and does not need to be serializable.
    [ComVisible(true)]
    public static class Registry {
        static Registry()
        { 
        }

        /**
         * Current User Key.
         * 
         * This key should be used as the root for all user specific settings.
         */
        public static readonly RegistryKey CurrentUser        = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_USER);
        
        /**
         * Local Machine Key.
         * 
         * This key should be used as the root for all machine specific settings.
         */
        public static readonly RegistryKey LocalMachine       = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE);
        
        /**
         * Classes Root Key.
         * 
         * This is the root key of class information.
         */
        public static readonly RegistryKey ClassesRoot        = RegistryKey.GetBaseKey(RegistryKey.HKEY_CLASSES_ROOT);
        
        /**
         * Users Root Key.
         * 
         * This is the root of users.
         */
        public static readonly RegistryKey Users              = RegistryKey.GetBaseKey(RegistryKey.HKEY_USERS);
        
        /**
         * Performance Root Key.
         * 
         * This is where dynamic performance data is stored on NT.
         */
        public static readonly RegistryKey PerformanceData    = RegistryKey.GetBaseKey(RegistryKey.HKEY_PERFORMANCE_DATA);
        
        /**
         * Current Config Root Key.
         * 
         * This is where current configuration information is stored.
         */
        public static readonly RegistryKey CurrentConfig      = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_CONFIG);

        //
        // Following function will parse a keyName and returns the basekey for it.
        // It will also store the subkey name in the out parameter.
        // If the keyName is not valid, we will throw ArgumentException.
        // The return value shouldn't be null. 
        //
        private static RegistryKey GetBaseKeyFromKeyName(string keyName, out string subKeyName) {
            if( keyName == null) {
                throw new ArgumentNullException(nameof(keyName));
            }

            string basekeyName;
            int i = keyName.IndexOf('\\');
            if( i != -1) {
                basekeyName = keyName.Substring(0, i).ToUpper(System.Globalization.CultureInfo.InvariantCulture);
            }
            else {
                basekeyName = keyName.ToUpper(System.Globalization.CultureInfo.InvariantCulture);
            }                        
            RegistryKey basekey = null;

            switch(basekeyName) {
                case "HKEY_CURRENT_USER": 
                    basekey = Registry.CurrentUser;
                    break;
                case "HKEY_LOCAL_MACHINE": 
                    basekey = Registry.LocalMachine;
                    break;
                case "HKEY_CLASSES_ROOT": 
                    basekey = Registry.ClassesRoot;
                    break;
                case "HKEY_USERS": 
                    basekey = Registry.Users;
                    break;
                case "HKEY_PERFORMANCE_DATA": 
                    basekey = Registry.PerformanceData;
                    break;
                case "HKEY_CURRENT_CONFIG": 
                    basekey = Registry.CurrentConfig;
                    break;
                default:
                    throw new ArgumentException(Environment.GetResourceString("Arg_RegInvalidKeyName", nameof(keyName)));
            }            
            if( i == -1 || i == keyName.Length) {
                subKeyName = string.Empty;
            }
            else {
                subKeyName = keyName.Substring(i + 1, keyName.Length - i - 1);
            }
            return basekey;
        }

        public static object GetValue(string keyName, string valueName, object defaultValue ) {
            string subKeyName;
            RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
            BCLDebug.Assert(basekey != null, "basekey can't be null.");
            RegistryKey key = basekey.OpenSubKey(subKeyName);
            if(key == null) { // if the key doesn't exist, do nothing
                return null;
            }
            try {
                return key.GetValue(valueName, defaultValue);                
            }            
            finally {
                key.Close();
            }            
        }
        
        public static void SetValue(string keyName, string valueName, object value ) {
            SetValue(keyName, valueName, value, RegistryValueKind.Unknown);            
        }
        
        public static void SetValue(string keyName, string valueName, object value, RegistryValueKind valueKind ) {
            string subKeyName; 
            RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
            BCLDebug.Assert(basekey != null, "basekey can't be null!");                
            RegistryKey key = basekey.CreateSubKey(subKeyName);
            BCLDebug.Assert(key != null, "An exception should be thrown if failed!");
            try {
                key.SetValue(valueName, value, valueKind);            
            }
            finally {
                key.Close();
            }
        }                
    }
}