using System; using System.Linq; using Microsoft.Win32; namespace mRemoteNG.Tools.WindowsRegistry { public class WindowsRegistry : IRegistry { public string[] GetSubKeyNames(RegistryHive hive, string keyPath) { keyPath.ThrowIfNull(nameof(keyPath)); using (var key = OpenSubKey(hive, keyPath)) { return key.Any() ? key.First().GetSubKeyNames() : new string[0]; } } public Optional GetKeyValue(RegistryHive hive, string keyPath, string propertyName) { keyPath.ThrowIfNull(nameof(keyPath)); propertyName.ThrowIfNull(nameof(propertyName)); using (var key = OpenSubKey(hive, keyPath)) { if (!key.Any()) return Optional.Empty; return key.First().GetValue(propertyName) as string; } } private DisposableOptional OpenSubKey(RegistryHive hive, string keyPath) { switch (hive) { case RegistryHive.ClassesRoot: return new DisposableOptional(Registry.ClassesRoot.OpenSubKey(keyPath)); case RegistryHive.CurrentConfig: return new DisposableOptional(Registry.CurrentConfig.OpenSubKey(keyPath)); case RegistryHive.CurrentUser: return new DisposableOptional(Registry.CurrentUser.OpenSubKey(keyPath)); case RegistryHive.Users: return new DisposableOptional(Registry.Users.OpenSubKey(keyPath)); case RegistryHive.LocalMachine: return new DisposableOptional(Registry.LocalMachine.OpenSubKey(keyPath)); default: throw new ArgumentOutOfRangeException(nameof(hive), hive, null); } } } }