public static void ImportSharedSettings(this ApplicationSettingsBase settings, string configurationFilename)
        {
            SystemConfiguration configuration = SystemConfigurationHelper.GetExeConfiguration(configurationFilename);
            var values = SystemConfigurationHelper.GetSettingsValues(configuration, settings.GetType());

            SetSharedPropertyValues(settings, values);
        }
        public static SettingsPropertyValueCollection GetSharedPropertyValues(LocalFileSettingsProvider provider, SettingsContext context, SettingsPropertyCollection properties, string currentExeConfigFilename = null)
        {
            var settingsClass = (Type)context["SettingsClassType"];

            var systemConfiguration = String.IsNullOrEmpty(currentExeConfigFilename)
                              ? SystemConfigurationHelper.GetExeConfiguration()
                              : SystemConfigurationHelper.GetExeConfiguration(currentExeConfigFilename);

            var storedValues = systemConfiguration.GetSettingsValues(settingsClass);

            // Create new collection of values
            var values = new SettingsPropertyValueCollection();

            foreach (SettingsProperty setting in properties)
            {
                var value = new SettingsPropertyValue(setting)
                {
                    SerializedValue = storedValues.ContainsKey(setting.Name) ? storedValues[setting.Name] : null,
                    IsDirty         = false
                };

                // use the stored value, or set the SerializedValue to null, which tells .NET to use the default value
                values.Add(value);
            }

            return(values);
        }
        public static void SetSharedPropertyValues(LocalFileSettingsProvider provider, SettingsContext context, SettingsPropertyValueCollection values, string currentExeConfigFilename)
        {
            var valuesToStore = new Dictionary <string, string>();

            foreach (SettingsPropertyValue value in values)
            {
                if (value.SerializedValue != null)
                {
                    valuesToStore[value.Name] = (string)value.SerializedValue;
                }
            }

            var systemConfiguration = String.IsNullOrEmpty(currentExeConfigFilename)
                                          ? SystemConfigurationHelper.GetExeConfiguration()
                                          : SystemConfigurationHelper.GetExeConfiguration(currentExeConfigFilename);

            var settingsClass = (Type)context["SettingsClassType"];

            systemConfiguration.PutSettingsValues(settingsClass, valuesToStore);

            foreach (SettingsPropertyValue value in values)
            {
                value.IsDirty = false;
            }
        }
        private IDictionary <string, string> GetCriticalSettingsValues(Type settingsClassType)
        {
            // if the critical settings file doesn't exist or we couldn't figure out the path for some reason, fail silently
            if (string.IsNullOrEmpty(_criticalSettingsPath) || !File.Exists(_criticalSettingsPath))
            {
                return(new Dictionary <string, string>(0));
            }

            var criticalConfiguration = SystemConfigurationHelper.GetExeConfiguration(_criticalSettingsPath);

            return(SystemConfigurationHelper.GetSettingsValues(criticalConfiguration, settingsClassType));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationCriticalSettingsProvider"/> class.
        /// </summary>
        public ApplicationCriticalSettingsProvider()
        {
            try
            {
                var baseAppConfig = SystemConfigurationHelper.GetExeConfiguration().FilePath;

#if DEBUG
                const string vshostExeConfigExtension = ".vshost.exe.config";
                if (baseAppConfig.EndsWith(vshostExeConfigExtension, StringComparison.InvariantCultureIgnoreCase))
                {
                    baseAppConfig = string.Format("{0}.exe.config", baseAppConfig.Substring(0, baseAppConfig.Length - vshostExeConfigExtension.Length));
                }
#endif

                _criticalSettingsPath = Path.ChangeExtension(baseAppConfig, "critical.config");
            }
            catch (Exception)
            {
                // SystemConfigurationHelper.GetExeConfiguration() will throw exception when used in ASP.NET
                // Use the default filename instead.
                _criticalSettingsPath = Path.Combine(Platform.InstallDirectory, _defaultConfigFilename);
            }
        }