Merge pull request #3262 from lindenlab/add_continuations_test
[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 using System;
33 using System.Runtime.InteropServices;
34
35 namespace Microsoft.Win32
36 {
37         [ComVisible (true)]
38         public static class Registry
39         {
40                 public static readonly RegistryKey ClassesRoot = new RegistryKey (
41                                 RegistryHive.ClassesRoot);
42                 public static readonly RegistryKey CurrentConfig = new RegistryKey (
43                                 RegistryHive.CurrentConfig);
44                 public static readonly RegistryKey CurrentUser = new RegistryKey (
45                                 RegistryHive.CurrentUser);
46
47                 [Obsolete ("Use PerformanceData instead")]
48                 public static readonly RegistryKey DynData = new RegistryKey (
49                                 RegistryHive.DynData);
50                 public static readonly RegistryKey LocalMachine = new RegistryKey (
51                                 RegistryHive.LocalMachine);
52                 public static readonly RegistryKey PerformanceData = new RegistryKey (
53                                 RegistryHive.PerformanceData);
54                 public static readonly RegistryKey Users = new RegistryKey (
55                                 RegistryHive.Users);
56
57                 static RegistryKey ToKey (string keyName, bool setting)
58                 {
59                         if (keyName == null)
60                                 throw new ArgumentException ("Not a valid registry key name", "keyName");
61
62                         RegistryKey key = null;
63                         string [] keys = keyName.Split ('\\');
64
65                         switch (keys [0]){
66                         case "HKEY_CLASSES_ROOT":
67                                 key = ClassesRoot;
68                                 break;
69                         case "HKEY_CURRENT_CONFIG":
70                                 key = CurrentConfig;
71                                 break;
72                         case "HKEY_CURRENT_USER":
73                                 key = CurrentUser;
74                                 break;
75                         case "HKEY_DYN_DATA":
76                                 key = DynData;
77                                 break;
78                         case "HKEY_LOCAL_MACHINE":
79                                 key = LocalMachine;
80                                 break;
81                         case "HKEY_PERFORMANCE_DATA":
82                                 key = PerformanceData;
83                                 break;
84                         case "HKEY_USERS":
85                                 key = Users;
86                                 break;
87                         default:
88                                 throw new ArgumentException ("Keyname does not start with a valid registry root", "keyName");
89                         }
90
91                         for (int i = 1; i < keys.Length; i++){
92                                 RegistryKey nkey = key.OpenSubKey (keys [i], setting);
93                                 if (nkey == null){
94                                         if (!setting)
95                                                 return null;
96                                         nkey = key.CreateSubKey (keys [i]);
97                                 }
98                                 key = nkey;
99                         }
100                         return key;
101                 }
102                 
103                 public static void SetValue (string keyName, string valueName, object value)
104                 {
105                         RegistryKey key = ToKey (keyName, true);
106                         if (valueName.Length > 255)
107                                 throw new ArgumentException ("valueName is larger than 255 characters", "valueName");
108
109                         if (key == null)
110                                 throw new ArgumentException ("cant locate that keyName", "keyName");
111
112                         key.SetValue (valueName, value);
113                 }
114
115                 public static void SetValue (string keyName, string valueName, object value, RegistryValueKind valueKind)
116                 {
117                         RegistryKey key = ToKey (keyName, true);
118                         if (valueName.Length > 255)
119                                 throw new ArgumentException ("valueName is larger than 255 characters", "valueName");
120
121                         if (key == null)
122                                 throw new ArgumentException ("cant locate that keyName", "keyName");
123
124                         key.SetValue (valueName, value, valueKind);
125                 }
126                 
127                 public static object GetValue (string keyName, string valueName, object defaultValue)
128                 {
129                         RegistryKey key = ToKey (keyName, false);
130                         if (key == null)
131                                 return defaultValue;
132                         
133                         return key.GetValue (valueName, defaultValue);
134                 }
135         }
136 }
137