Пример #1
0
        /// <summary>
        /// Attempts to set the value of an option in the settings mapping to the supplied value.
        /// If the option fails to get set, it will keep its default value.
        /// </summary>
        /// <param name="optionName">The name of the option in the mapping to be changed.</param>
        /// <param name="value">The value to attempt to set the option to.</param>
        /// <returns>Returns an error message detailing why it failed to be set, or null if it was sucessfully set.</returns>
        public void SetOption(string optionName, Object value)
        {
            ValidationResponse validationResult = _settingsMapping[optionName].SetOptionValue(value);

            if (!validationResult.Successful)
            {
                string exString = $"Failed to set option '{optionName}', reason: '{validationResult.Information}'. Option will keep its current value.";
                throw new RegOptionAssignmentException(exString);
            }
        }
Пример #2
0
        /// <summary>
        /// Attempt to set the current value of the option to the passed one.
        /// </summary>
        /// <param name="value">The value to be attempted.</param>
        /// <returns>A ValidationResponse instance which contains the result of the validation.</returns>
        internal ValidationResponse SetOptionValue(Object value)
        {
            ValidationResponse validationResponse = Validate(value);

            if (validationResponse.Successful)
            {
                _optionValue = Convert.ChangeType(value.ToString(), _optionType);
            }

            return(validationResponse);
        }
Пример #3
0
        /// <summary>
        /// Attempts to load values out of the registry and set the option instance's values with the loaded values.
        /// </summary>
        /// <returns>Null if successful, or a string detailing which options failed and why.</returns>
        public void LoadSettings() // Load settings from the registry instance
        {
            string loadResult = null;

            foreach (KeyValuePair <string, RegOption> kvp in _settingsMapping)
            {
                string subKeys = kvp.Value.GetSubKeys();
                string keyPath = _registryString;

                if (subKeys != null)
                {
                    keyPath += subKeys;
                }

                Object keyValue;
                try
                {
                    keyValue = Registry.GetValue(keyPath, kvp.Value.GetKeyName(), kvp.Value.OptionValue);
                    if (keyValue == null)
                    {
                        loadResult += $"\r\nFailed loading option '{kvp.Value.GetKeyName()}': Option did not exist in the registry. " +
                                      $"The value will use its default.";
                    }
                    else
                    {
                        ValidationResponse validation_result = kvp.Value.SetOptionValue(keyValue);
                        if (!validation_result.Successful)
                        {
                            loadResult += $"\r\nFailed when validating an option while loading: '{kvp.Value.GetKeyName()}' - " +
                                          $"'{validation_result.Information}'. The value will use its default.";
                        }
                    }
                }
                catch (FormatException)
                {
                    loadResult += $"\r\nFailed when loading option '{kvp.Value.GetKeyName()}': Option was not formatted correctly. " +
                                  "This usually occurs if someone manually" + "alters the entry in the registry. The value will use its default.";
                }
            }

            if (loadResult != null)
            {
                throw new RegLoadException(loadResult);
            }
        }