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