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