namespace WindowsDisplayAPI { /// /// Represents a Windows Video Device including Display Devices and Video Controllers /// public abstract class Device { /// /// Creates a new Device /// /// The device path /// The device name /// The device driver registry key protected Device(string devicePath, string deviceName, string deviceKey) { DevicePath = devicePath; DeviceName = deviceName; DeviceKey = deviceKey; } /// /// Gets the registry address of the device driver and configuration /// public virtual string DeviceKey { get; } /// /// Gets the Windows device name /// public virtual string DeviceName { get; } /// /// Gets the Windows device path /// public virtual string DevicePath { get; } /// public override string ToString() { return $"{GetType().Name}: {DeviceName}"; } #if !NETSTANDARD /// /// Opens the registry key at the address specified by the DeviceKey property /// /// A RegistryKey instance for successful call, otherwise null /// Registry address is invalid or unknown. public Microsoft.Win32.RegistryKey OpenDeviceKey() { if (string.IsNullOrWhiteSpace(DeviceKey)) { return null; } const string machineRootName = "\\Registry\\Machine\\"; const string userRootName = "\\Registry\\Current\\"; if (DeviceKey.StartsWith(machineRootName, System.StringComparison.InvariantCultureIgnoreCase)) { return Microsoft.Win32.Registry.LocalMachine.OpenSubKey( DeviceKey.Substring(machineRootName.Length), Microsoft.Win32.RegistryKeyPermissionCheck.ReadSubTree ); } if (DeviceKey.StartsWith(userRootName, System.StringComparison.InvariantCultureIgnoreCase)) { return Microsoft.Win32.Registry.Users.OpenSubKey( DeviceKey.Substring(userRootName.Length), Microsoft.Win32.RegistryKeyPermissionCheck.ReadSubTree ); } throw new Exceptions.InvalidRegistryAddressException("Registry address is invalid or unknown."); } /// /// Opens the registry key of the Windows PnP manager for this device /// /// A RegistryKey instance for successful call, otherwise null public Microsoft.Win32.RegistryKey OpenDevicePnPKey() { if (string.IsNullOrWhiteSpace(DevicePath)) { return null; } var path = DevicePath; if (path.StartsWith("\\\\?\\")) { path = path.Substring(4).Replace("#", "\\"); if (path.EndsWith("}")) { var guidIndex = path.LastIndexOf("{", System.StringComparison.InvariantCulture); if (guidIndex > 0) { path = path.Substring(0, guidIndex); } } } return Microsoft.Win32.Registry.LocalMachine.OpenSubKey( "SYSTEM\\CurrentControlSet\\Enum\\" + path, Microsoft.Win32.RegistryKeyPermissionCheck.ReadSubTree ); } #endif } }