Initial commit
[mono.git] / mcs / class / referencesource / mscorlib / microsoft / win32 / registry.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 #if FEATURE_WIN32_REGISTRY    
7
8 namespace Microsoft.Win32 {
9     using System;
10     using System.Runtime.InteropServices;
11     using System.Runtime.Versioning;
12
13 #if !FEATURE_PAL
14
15     /**
16      * Registry encapsulation. Contains members representing all top level system
17      * keys.
18      *
19      * @security(checkClassLinking=on)
20      */
21     //This class contains only static members and does not need to be serializable.
22     [ComVisible(true)]
23     public static class Registry {
24         [System.Security.SecuritySafeCritical]  // auto-generated
25         static Registry()
26         { 
27         }
28
29         /**
30          * Current User Key.
31          * 
32          * This key should be used as the root for all user specific settings.
33          */
34         [ResourceExposure(ResourceScope.Machine)]
35         public static readonly RegistryKey CurrentUser        = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_USER);
36         
37         /**
38          * Local Machine Key.
39          * 
40          * This key should be used as the root for all machine specific settings.
41          */
42         [ResourceExposure(ResourceScope.Machine)]
43         public static readonly RegistryKey LocalMachine       = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE);
44         
45         /**
46          * Classes Root Key.
47          * 
48          * This is the root key of class information.
49          */
50         [ResourceExposure(ResourceScope.Machine)]
51         public static readonly RegistryKey ClassesRoot        = RegistryKey.GetBaseKey(RegistryKey.HKEY_CLASSES_ROOT);
52         
53         /**
54          * Users Root Key.
55          * 
56          * This is the root of users.
57          */
58         [ResourceExposure(ResourceScope.Machine)]
59         public static readonly RegistryKey Users              = RegistryKey.GetBaseKey(RegistryKey.HKEY_USERS);
60         
61         /**
62          * Performance Root Key.
63          * 
64          * This is where dynamic performance data is stored on NT.
65          */
66         [ResourceExposure(ResourceScope.Machine)]
67         public static readonly RegistryKey PerformanceData    = RegistryKey.GetBaseKey(RegistryKey.HKEY_PERFORMANCE_DATA);
68         
69         /**
70          * Current Config Root Key.
71          * 
72          * This is where current configuration information is stored.
73          */
74         [ResourceExposure(ResourceScope.Machine)]
75         public static readonly RegistryKey CurrentConfig      = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_CONFIG);
76         
77         /**
78          * Dynamic Data Root Key.
79          * 
80          * LEGACY: This is where dynamic performance data is stored on Win9X.
81          * This does not exist on NT.
82          */
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);
86
87         //
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. 
92         //
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");
97             }
98
99             string basekeyName;
100             int i = keyName.IndexOf('\\');
101             if( i != -1) {
102                 basekeyName = keyName.Substring(0, i).ToUpper(System.Globalization.CultureInfo.InvariantCulture);
103             }
104             else {
105                 basekeyName = keyName.ToUpper(System.Globalization.CultureInfo.InvariantCulture);
106             }                        
107             RegistryKey basekey = null;
108
109             switch(basekeyName) {
110                 case "HKEY_CURRENT_USER": 
111                     basekey = Registry.CurrentUser;
112                     break;
113                 case "HKEY_LOCAL_MACHINE": 
114                     basekey = Registry.LocalMachine;
115                     break;
116                 case "HKEY_CLASSES_ROOT": 
117                     basekey = Registry.ClassesRoot;
118                     break;
119                 case "HKEY_USERS": 
120                     basekey = Registry.Users;
121                     break;
122                 case "HKEY_PERFORMANCE_DATA": 
123                     basekey = Registry.PerformanceData;
124                     break;
125                 case "HKEY_CURRENT_CONFIG": 
126                     basekey = Registry.CurrentConfig;
127                     break;
128                 case "HKEY_DYN_DATA": 
129                     basekey = RegistryKey.GetBaseKey(RegistryKey.HKEY_DYN_DATA);
130                     break;                    
131                 default:
132                     throw new ArgumentException(Environment.GetResourceString("Arg_RegInvalidKeyName", "keyName"));
133             }            
134             if( i == -1 || i == keyName.Length) {
135                 subKeyName = string.Empty;
136             }
137             else {
138                 subKeyName = keyName.Substring(i + 1, keyName.Length - i - 1);
139             }
140             return basekey;
141         }
142
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 ) {
147             string subKeyName;
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
152                 return null;
153             }
154             try {
155                 return key.GetValue(valueName, defaultValue);                
156             }            
157             finally {
158                 key.Close();
159             }            
160         }
161         
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);            
166         }
167         
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 ) {
172             string subKeyName; 
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!");
177             try {
178                 key.SetValue(valueName, value, valueKind);            
179             }
180             finally {
181                 key.Close();
182             }
183         }                
184     }
185
186 #endif // !FEATURE_PAL
187 }
188 #endif // FEATURE_WIN32_REGISTRY    
189
190