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