Registry
Provides RegistryKey objects that represent the root keys in the Windows registry, and static methods to access key/value pairs.
using System;
using System.Runtime.CompilerServices;
namespace Microsoft.Win32
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class Registry
{
public static readonly RegistryKey CurrentUser = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default);
public static readonly RegistryKey LocalMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);
public static readonly RegistryKey ClassesRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Default);
public static readonly RegistryKey Users = RegistryKey.OpenBaseKey(RegistryHive.Users, RegistryView.Default);
public static readonly RegistryKey PerformanceData = RegistryKey.OpenBaseKey(RegistryHive.PerformanceData, RegistryView.Default);
public static readonly RegistryKey CurrentConfig = RegistryKey.OpenBaseKey(RegistryHive.CurrentConfig, RegistryView.Default);
private static RegistryKey GetBaseKeyFromKeyName(string keyName, out string subKeyName)
{
if (keyName == null)
throw new ArgumentNullException("keyName");
int num = keyName.IndexOf('\\');
int num2 = (num != -1) ? num : keyName.Length;
RegistryKey registryKey = null;
switch (num2) {
case 10:
registryKey = Users;
break;
case 17:
registryKey = ((char.ToUpperInvariant(keyName[6]) == 'L') ? ClassesRoot : CurrentUser);
break;
case 18:
registryKey = LocalMachine;
break;
case 19:
registryKey = CurrentConfig;
break;
case 21:
registryKey = PerformanceData;
break;
}
if (registryKey != null && keyName.StartsWith(registryKey.Name, StringComparison.OrdinalIgnoreCase)) {
subKeyName = ((num == -1 || num == keyName.Length) ? string.Empty : keyName.Substring(num + 1, keyName.Length - num - 1));
return registryKey;
}
throw new ArgumentException(System.SR.Format(System.SR.Arg_RegInvalidKeyName, "keyName"), "keyName");
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static object GetValue([System.Runtime.CompilerServices.Nullable(1)] string keyName, string valueName, object defaultValue)
{
string subKeyName;
RegistryKey baseKeyFromKeyName = GetBaseKeyFromKeyName(keyName, out subKeyName);
using (RegistryKey registryKey = baseKeyFromKeyName.OpenSubKey(subKeyName))
return registryKey?.GetValue(valueName, defaultValue);
}
public static void SetValue(string keyName, [System.Runtime.CompilerServices.Nullable(2)] string valueName, object value)
{
SetValue(keyName, valueName, value, RegistryValueKind.Unknown);
}
public static void SetValue(string keyName, [System.Runtime.CompilerServices.Nullable(2)] string valueName, object value, RegistryValueKind valueKind)
{
string subKeyName;
RegistryKey baseKeyFromKeyName = GetBaseKeyFromKeyName(keyName, out subKeyName);
using (RegistryKey registryKey = baseKeyFromKeyName.CreateSubKey(subKeyName))
registryKey.SetValue(valueName, value, valueKind);
}
}
}