2 // Microsoft.Win32.Registry.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // stubbed out by Alexandre Pigolkine (pigolkine@gmx.de)
10 // Copyright (C) 2004, 2005 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
35 using System.Runtime.InteropServices;
37 namespace Microsoft.Win32
40 public static class Registry
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);
50 [Obsolete ("Use PerformanceData instead")]
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 (
61 static RegistryKey ToKey (string keyName, bool setting)
64 throw new ArgumentException ("Not a valid registry key name", "keyName");
66 RegistryKey key = null;
67 string [] keys = keyName.Split ('\\');
70 case "HKEY_CLASSES_ROOT":
73 case "HKEY_CURRENT_CONFIG":
76 case "HKEY_CURRENT_USER":
82 case "HKEY_LOCAL_MACHINE":
85 case "HKEY_PERFORMANCE_DATA":
86 key = PerformanceData;
92 throw new ArgumentException ("Keyname does not start with a valid registry root", "keyName");
95 for (int i = 1; i < keys.Length; i++){
96 RegistryKey nkey = key.OpenSubKey (keys [i], true);
100 nkey = key.CreateSubKey (keys [i]);
107 public static void SetValue (string keyName, string valueName, object value)
109 RegistryKey key = ToKey (keyName, true);
110 if (valueName.Length > 255)
111 throw new ArgumentException ("valueName is larger than 255 characters", "valueName");
114 throw new ArgumentException ("cant locate that keyName", "keyName");
116 key.SetValue (valueName, value);
119 public static void SetValue (string keyName, string valueName, object value, RegistryValueKind valueKind)
121 RegistryKey key = ToKey (keyName, true);
122 if (valueName.Length > 255)
123 throw new ArgumentException ("valueName is larger than 255 characters", "valueName");
126 throw new ArgumentException ("cant locate that keyName", "keyName");
128 key.SetValue (valueName, value, valueKind);
131 public static object GetValue (string keyName, string valueName, object defaultValue)
133 RegistryKey key = ToKey (keyName, false);
137 return key.GetValue (valueName, defaultValue);