Пример #1
0
        public override object Load(ChoBaseConfigurationElement configElement, XmlNode node)
        {
            base.Load(configElement, node);

            ConfigurationManager.RefreshSection(APP_SETTINGS_SECTION_NAME);
            ChoConfigurationManager.Refresh();
            ConfigFilePath = ChoConfigurationManager.ApplicationConfigurationFilePath;
            string appSettingsFilePath = GetAppSettingsFilePathIfAnySpecified();

            configElement[ChoConfigurationConstants.FORCE_PERSIST] = CanForcePersist(appSettingsFilePath);

            ConfigSectionName = APP_SETTINGS_SECTION_NAME;
            RefreshSection(appSettingsFilePath);
            return(ChoConfigurationManager.ApplicationConfiguration.AppSettings.Settings.ToNameValueCollection());
        }
Пример #2
0
        public override void Persist(object data, ChoDictionaryService <string, object> stateInfo)
        {
            if (!(data is NameValueCollection))
            {
                throw new ChoConfigurationException("Data object is not NameValueCollection object.");
            }
            try
            {
                //Write meta-data info
                ChoConfigurationMetaDataManager.SetMetaDataSection(ConfigElement);
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ConfigElement.Log(ex.ToString());
            }

            bool   saveMainConfig      = false;
            string appSettingsFilePath = this.GetAppSettingsFilePathIfAnySpecified();

            if (!appSettingsFilePath.IsNullOrWhiteSpace())
            {
                if (ChoConfigurationManager.GetFullPath(ChoConfigurationManager.ApplicationConfiguration.AppSettings.File) != ChoConfigurationManager.GetFullPath(appSettingsFilePath))
                {
                    saveMainConfig = true;
                    ChoConfigurationManager.ApplicationConfiguration.AppSettings.File = appSettingsFilePath;
                }
            }

            NameValueCollection nameValueCollection = ((NameValueCollection)data);

            if (ChoConfigurationManager.ApplicationConfiguration.AppSettings.File.IsNullOrWhiteSpace())
            {
                foreach (string key1 in nameValueCollection.Keys)
                {
                    if (ChoConfigurationManager.ApplicationConfiguration.AppSettings.Settings.AllKeys.Contains(key1))
                    {
                        ChoConfigurationManager.ApplicationConfiguration.AppSettings.Settings[key1].Value = nameValueCollection[key1];
                    }
                    else
                    {
                        ChoConfigurationManager.ApplicationConfiguration.AppSettings.Settings.Add(new KeyValueConfigurationElement(key1, nameValueCollection[key1]));
                    }
                }

                ChoConfigurationManager.ApplicationConfiguration.Save(ConfigurationSaveMode.Modified);
            }
            else
            {
                if (saveMainConfig)
                {
                    ChoConfigurationManager.ApplicationConfiguration.Save(ConfigurationSaveMode.Modified);
                }
                File.WriteAllText(ChoConfigurationManager.GetFullPath(ChoConfigurationManager.ApplicationConfiguration.AppSettings.File), GetAppSettingsText(nameValueCollection));
            }
            //ConfigurationManager.RefreshSection(APP_SETTINGS_SECTION_NAME);
            ChoConfigurationManager.Refresh();
        }