Exemplo n.º 1
0
        public static GenericRegistryValue get_value(RegistryHiveType hive, string subKeyPath, string registryValue)
        {
            var hiveActual = (RegistryHive)Enum.Parse(typeof(RegistryHive), hive.to_string(), ignoreCase: true);
            IList <RegistryKey> keyLocations = new List <RegistryKey>();

            if (Environment.Is64BitOperatingSystem)
            {
                keyLocations.Add(RegistryKey.OpenBaseKey(hiveActual, RegistryView.Registry64));
            }

            keyLocations.Add(RegistryKey.OpenBaseKey(hiveActual, RegistryView.Registry32));

            GenericRegistryValue value = null;

            foreach (var topLevelRegistryKey in keyLocations)
            {
                using (topLevelRegistryKey)
                {
                    var key = topLevelRegistryKey.OpenSubKey(subKeyPath, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey);
                    if (key != null)
                    {
                        value = FaultTolerance.try_catch_with_logging_exception(
                            () =>
                        {
                            if (key.GetValueNames().Contains(registryValue, StringComparer.InvariantCultureIgnoreCase))
                            {
                                return(new GenericRegistryValue
                                {
                                    Name = registryValue,
                                    ParentKeyName = key.Name,
                                    Type = (RegistryValueKindType)Enum.Parse(typeof(RegistryValueKindType), key.GetValueKind(registryValue).to_string(), ignoreCase: true),
                                    Value = key.get_value_as_string(registryValue),
                                });
                            }

                            return(null);
                        },
                            "Could not get registry value '{0}' from key '{1}'".format_with(registryValue, key.Name),
                            logWarningInsteadOfError: true);

                        if (value != null)
                        {
                            break;
                        }
                    }
                }
            }

            return(value);
        }
Exemplo n.º 2
0
        public static GenericRegistryValue get_value(RegistryHiveType hive, string subKeyPath, string registryValue)
        {
            var hiveActual = (RegistryHive)Enum.Parse(typeof(RegistryHive), hive.to_string(), ignoreCase: true);
            IList<RegistryKey> keyLocations = new List<RegistryKey>();
            if (Environment.Is64BitOperatingSystem)
            {
                keyLocations.Add(RegistryKey.OpenBaseKey(hiveActual, RegistryView.Registry64));
            }

            keyLocations.Add(RegistryKey.OpenBaseKey(hiveActual, RegistryView.Registry32));

            GenericRegistryValue value = null;

            foreach (var topLevelRegistryKey in keyLocations)
            {
                using (topLevelRegistryKey)
                {
                    var key = topLevelRegistryKey.OpenSubKey(subKeyPath, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey);
                    if (key != null)
                    {
                        value = FaultTolerance.try_catch_with_logging_exception(
                            () =>
                            {
                                if (key.GetValueNames().Contains(registryValue, StringComparer.InvariantCultureIgnoreCase))
                                {
                                    return new GenericRegistryValue
                                    {
                                        Name = registryValue,
                                        ParentKeyName = key.Name,
                                        Type = (RegistryValueKindType)Enum.Parse(typeof(RegistryValueKindType), key.GetValueKind(registryValue).to_string(), ignoreCase: true),
                                        Value = key.get_value_as_string(registryValue),
                                    };
                                }

                                return null;
                            },
                            "Could not get registry value '{0}' from key '{1}'".format_with(registryValue, key.Name),
                            logWarningInsteadOfError: true);

                        if (value != null) break;
                    }
                }
            }

            return value;
        }