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); }
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; } } }
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); } }
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(); }