示例#1
0
        private bool CanForcePersist(string appSettingsFilePath)
        {
            if (appSettingsFilePath.IsNullOrWhiteSpace())
            {
                return(false);
            }

            appSettingsFilePath = ChoConfigurationManager.GetFullPath(appSettingsFilePath);
            if (!File.Exists(appSettingsFilePath))
            {
                return(true);
            }

            //try
            //{
            //    XDocument doc = XDocument.Load(appSettingsFilePath);
            //    if (doc.Root == null
            //    || doc.Root.Name.LocalName != APP_SETTINGS_SECTION_NAME)
            //        return true;
            //}
            //catch
            //{
            //    return true;
            //}

            return(false);
        }
示例#2
0
        private void RefreshSection(string appSettingsFilePath)
        {
            if (!appSettingsFilePath.IsNullOrWhiteSpace())
            {
                if (ChoConfigurationManager.GetFullPath(ChoConfigurationManager.ApplicationConfiguration.AppSettings.File) != ChoConfigurationManager.GetFullPath(appSettingsFilePath))
                {
                    ConfigElement[ChoConfigurationConstants.FORCE_PERSIST] = true;
                }
            }

            if (!ChoConfigurationManager.ApplicationConfiguration.AppSettings.File.IsNullOrWhiteSpace())
            {
                if (!File.Exists(ChoConfigurationManager.GetFullPath(ChoConfigurationManager.ApplicationConfiguration.AppSettings.File)))
                {
                    ConfigElement[ChoConfigurationConstants.FORCE_PERSIST] = true;
                }
            }
        }
示例#3
0
        private string GetAppSettingsFilePathIfAnySpecified()
        {
            string filePath = null;

            System.Configuration.AppSettingsSection appSettings = ChoConfigurationManager.ApplicationConfiguration.AppSettings;
            if (!appSettings.File.IsNullOrEmpty())
            {
                filePath = appSettings.File.NTrim();
            }
            else
            {
                filePath = ConfigElement.ConfigFilePath.NTrim();
            }

            if (Directory.Exists(ChoConfigurationManager.GetFullPath(filePath)))
            {
                return(null);
            }
            else
            {
                return(filePath);
            }
        }
示例#4
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();
        }