[mini] bump AOT version - WrapperType changed
[mono.git] / mcs / class / corlib / Microsoft.Win32 / Registry.cs
1 //
2 // Microsoft.Win32.Registry.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@novell.com)
6 //   stubbed out by Alexandre Pigolkine (pigolkine@gmx.de)
7 //
8
9 //
10 // Copyright (C) 2004, 2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 #if !NET_2_1
33
34 using System;
35 using System.Runtime.InteropServices;
36
37 namespace Microsoft.Win32
38 {
39         [ComVisible (true)]
40         public static class Registry
41         {
42                 public static readonly RegistryKey ClassesRoot = new RegistryKey (
43                                 RegistryHive.ClassesRoot);
44                 public static readonly RegistryKey CurrentConfig = new RegistryKey (
45                                 RegistryHive.CurrentConfig);
46                 public static readonly RegistryKey CurrentUser = new RegistryKey (
47                                 RegistryHive.CurrentUser);
48
49                 [Obsolete ("Use PerformanceData instead")]
50                 public static readonly RegistryKey DynData = new RegistryKey (
51                                 RegistryHive.DynData);
52                 public static readonly RegistryKey LocalMachine = new RegistryKey (
53                                 RegistryHive.LocalMachine);
54                 public static readonly RegistryKey PerformanceData = new RegistryKey (
55                                 RegistryHive.PerformanceData);
56                 public static readonly RegistryKey Users = new RegistryKey (
57                                 RegistryHive.Users);
58
59                 static RegistryKey ToKey (string keyName, bool setting)
60                 {
61                         if (keyName == null)
62                                 throw new ArgumentException ("Not a valid registry key name", "keyName");
63
64                         RegistryKey key = null;
65                         string [] keys = keyName.Split ('\\');
66
67                         switch (keys [0]){
68                         case "HKEY_CLASSES_ROOT":
69                                 key = ClassesRoot;
70                                 break;
71                         case "HKEY_CURRENT_CONFIG":
72                                 key = CurrentConfig;
73                                 break;
74                         case "HKEY_CURRENT_USER":
75                                 key = CurrentUser;
76                                 break;
77                         case "HKEY_DYN_DATA":
78                                 key = DynData;
79                                 break;
80                         case "HKEY_LOCAL_MACHINE":
81                                 key = LocalMachine;
82                                 break;
83                         case "HKEY_PERFORMANCE_DATA":
84                                 key = PerformanceData;
85                                 break;
86                         case "HKEY_USERS":
87                                 key = Users;
88                                 break;
89                         default:
90                                 throw new ArgumentException ("Keyname does not start with a valid registry root", "keyName");
91                         }
92
93                         for (int i = 1; i < keys.Length; i++){
94                                 RegistryKey nkey = key.OpenSubKey (keys [i], setting);
95                                 if (nkey == null){
96                                         if (!setting)
97                                                 return null;
98                                         nkey = key.CreateSubKey (keys [i]);
99                                 }
100                                 key = nkey;
101                         }
102                         return key;
103                 }
104                 
105                 public static void SetValue (string keyName, string valueName, object value)
106                 {
107                         RegistryKey key = ToKey (keyName, true);
108                         if (valueName.Length > 255)
109                                 throw new ArgumentException ("valueName is larger than 255 characters", "valueName");
110
111                         if (key == null)
112                                 throw new ArgumentException ("cant locate that keyName", "keyName");
113
114                         key.SetValue (valueName, value);
115                 }
116
117                 public static void SetValue (string keyName, string valueName, object value, RegistryValueKind valueKind)
118                 {
119                         RegistryKey key = ToKey (keyName, true);
120                         if (valueName.Length > 255)
121                                 throw new ArgumentException ("valueName is larger than 255 characters", "valueName");
122
123                         if (key == null)
124                                 throw new ArgumentException ("cant locate that keyName", "keyName");
125
126                         key.SetValue (valueName, value, valueKind);
127                 }
128                 
129                 public static object GetValue (string keyName, string valueName, object defaultValue)
130                 {
131                         RegistryKey key = ToKey (keyName, false);
132                         if (key == null)
133                                 return defaultValue;
134                         
135                         return key.GetValue (valueName, defaultValue);
136                 }
137         }
138 }
139
140 #endif // NET_2_1
141