예제 #1
0
        public static void RemoveSettingsValues(this SystemConfiguration configuration, Type settingsClass, SettingScope?scope = null)
        {
            var removeApplicationSettings = !scope.HasValue || scope.Value == SettingScope.Application;
            var removeUserSettings        = !scope.HasValue || scope.Value == SettingScope.User;

            if (removeApplicationSettings)
            {
                var sectionPath = new ConfigurationSectionPath(settingsClass, SettingScope.Application);
                ConfigurationSectionGroup group = configuration.GetSectionGroup(sectionPath.GroupPath);
                if (group != null)
                {
                    group.Sections.Remove(sectionPath.SectionName);
                }
            }

            if (removeUserSettings)
            {
                var sectionPath = new ConfigurationSectionPath(settingsClass, SettingScope.User);
                var group       = configuration.GetSectionGroup(sectionPath.GroupPath);
                if (group != null)
                {
                    group.Sections.Remove(sectionPath.SectionName);
                }
            }

            configuration.Save(ConfigurationSaveMode.Minimal, true);
        }
예제 #2
0
        /// <summary>
        /// Stores the settings values for a given settings class.
        /// </summary>
        /// <param name="configuration">the configuration where the values will be stored</param>
        /// <param name="settingsClass">the settings class for which to store the values</param>
        /// <param name="dirtyValues">contains the values to be stored</param>
        public static void PutSettingsValues(this SystemConfiguration configuration, Type settingsClass, IDictionary <string, string> dirtyValues)
        {
            var applicationScopedProperties = GetProperties(settingsClass, SettingScope.Application);
            var userScopedProperties        = GetProperties(settingsClass, SettingScope.User);

            bool modified = false;

            if (applicationScopedProperties.Count > 0)
            {
                var sectionPath = new ConfigurationSectionPath(settingsClass, SettingScope.Application);
                modified = UpdateSection(configuration, sectionPath, applicationScopedProperties, dirtyValues);
            }
            if (userScopedProperties.Count > 0)
            {
                var sectionPath = new ConfigurationSectionPath(settingsClass, SettingScope.User);
                if (UpdateSection(configuration, sectionPath, userScopedProperties, dirtyValues))
                {
                    modified = true;
                }
            }

            if (modified)
            {
                configuration.Save(ConfigurationSaveMode.Minimal, true);
            }
        }
예제 #3
0
        /// <summary>
        /// Gets only those settings values that are different from the defaults for the given settings group.
        /// </summary>
        /// <param name="configuration">the configuration where the values will be taken from</param>
        /// <param name="settingsClass">the settings class for which to get the values</param>
        /// <param name="settingScope">the scope of the settings for which to get the values</param>
        public static Dictionary <string, string> GetSettingsValues(this SystemConfiguration configuration, Type settingsClass, SettingScope settingScope)
        {
            var properties  = GetProperties(settingsClass, settingScope);
            var sectionPath = new ConfigurationSectionPath(settingsClass, settingScope);

            return(GetSettingsValues(configuration, sectionPath, properties));
        }
예제 #4
0
        private static Dictionary <string, string> GetSettingsValues(
            SystemConfiguration configuration,
            ConfigurationSectionPath sectionPath,
            ICollection <PropertyInfo> properties)
        {
            var values = new Dictionary <string, string>();

            if (properties.Count > 0)
            {
                var section = sectionPath.GetSection(configuration);
                if (section != null)
                {
                    var clientSection = CastToClientSection(section);
                    foreach (PropertyInfo property in properties)
                    {
                        SettingElement element = GetSettingElement(clientSection, property, false);
                        if (element != null)                         //If the setting element is there, we'll assume it means the value is to be set.
                        {
                            values[property.Name] = GetElementValue(element);
                        }
                    }
                }
            }

            return(values);
        }
예제 #5
0
        private static bool UpdateSection(SystemConfiguration configuration, ConfigurationSectionPath sectionPath, IEnumerable <PropertyInfo> properties, IDictionary <string, string> newValues)
        {
            var section = sectionPath.GetSection(configuration);

            if (section != null)
            {
                return(UpdateSection(CastToClientSection(section), properties, newValues));
            }

            var group = sectionPath.GroupPath.GetSectionGroup(configuration, true);

            section = sectionPath.CreateSection();

            if (!UpdateSection(CastToClientSection(section), properties, newValues))
            {
                return(false);
            }

            group.Sections.Add(sectionPath.SectionName, section);
            return(true);
        }
        public IDictionary <string, string> GetSettingsValues(ConfigurationSectionPath path)
        {
            var values = new Dictionary <string, string>();

            if (_document.DocumentElement != null)
            {
                var element = _document.DocumentElement.SelectSingleNode(path) as XmlElement;
                if (element != null)
                {
                    foreach (XmlElement setting in element.ChildNodes)
                    {
                        var nameAttribute = setting.Attributes["name"];
                        if (nameAttribute == null)
                        {
                            continue;
                        }

                        var name = nameAttribute.Value;
                        if (String.IsNullOrEmpty(name))
                        {
                            continue;
                        }

                        var valueNode = setting.SelectSingleNode("value");
                        if (valueNode == null)
                        {
                            continue;
                        }

                        var serializeAsAttribute = setting.Attributes["serializeAs"];
                        var serializeAsValue     = serializeAsAttribute != null ? serializeAsAttribute.Value : String.Empty;
                        var serializeAs          = SystemConfigurationHelper.GetSerializeAs(serializeAsValue);
                        values.Add(name, SystemConfigurationHelper.GetElementValue(valueNode, serializeAs));
                    }
                }
            }

            return(values);
        }