Esempio n. 1
0
        /// <summary>
        ///     Opens the registry key at the address specified by the DeviceKey property
        /// </summary>
        /// <returns>A RegistryKey instance for successful call, otherwise null</returns>
        /// <exception cref="WindowsDisplayAPI.Exceptions.InvalidRegistryAddressException">Registry address is invalid or unknown.</exception>
        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.");
        }