/// <summary> /// Get the specified client settings section. /// </summary> /// <param name="sectionName">The path to the section to be returned.</param> /// <returns>Represents a group of user-scoped application settings in a configuration file.</returns> public static System.Configuration.ClientSettingsSection GetClientSettings(string sectionName) { // Get the section settings. System.Configuration.ClientSettingsSection configSetting = System.Configuration.ConfigurationManager.GetSection(sectionName) as System.Configuration.ClientSettingsSection; // Return the configuration type. return(configSetting); }
/// <summary> /// Get the specified client settings section. /// </summary> /// <param name="assemblyFilePath">The assembly file name and path assocciated with the configuration file.</param> /// <param name="sectionName">The path to the section to be returned.</param> /// <returns>Represents a group of user-scoped application settings in a configuration file.</returns> public static System.Configuration.ClientSettingsSection GetClientSettings(string assemblyFilePath, string sectionName) { // Get assembly configuration file. System.Configuration.Configuration config = Configuration(assemblyFilePath); // Get the section settings. System.Configuration.ClientSettingsSection configSetting = config.GetSection(sectionName) as System.Configuration.ClientSettingsSection; // Return the configuration type. return(configSetting); }
/// <summary> /// Sets the value for the specified key in the persistent storage (app.config, etc). /// <locDE><para />Schreibt den Wert für den angegebenen Schlüsselbegriff in den Konfigurationsspeicher (app.config, etc).</locDE> /// </summary> /// <param name="key">The key.<locDE><para />Der Schlüsselbegriff.</locDE></param> /// <param name="value">The value to store.<locDE><para />Der zu speichernde Wert.</locDE></param> public void SetValue(string key, string value) { if (string.IsNullOrWhiteSpace(key)) { return; } string writeInto = null; if (key.EndsWith("@appSettings")) { writeInto = "appSettings"; } else if (key.EndsWith("@applicationSettings")) { writeInto = "applicationSettings"; } else if (key.EndsWith("@userSettings")) { writeInto = "userSettings"; } else { GetValue(key, out writeInto); } #region TheAppName.Properties.Settings | userSettings if (writeInto.Equals("userSettings")) { try { //<userSettings> // <TheAppName.Properties.Settings> // <setting name="TheKey" serializeAs="String"> // <value>TheValue</value> // </setting> // </TheAppName.Properties.Settings> //</userSettings> #region Get Configuration instance System.Configuration.Configuration config = null; if (null != ConfigurationInstanceProvider) { config = ConfigurationInstanceProvider(); } else { config = System.Configuration.ConfigurationManager.OpenExeConfiguration(this.ConfigurationUserLevel); } #endregion System.Configuration.ClientSettingsSection clientSettingsSection = config.GetSection("userSettings/" + this.AppName + ".Properties.Settings") as System.Configuration.ClientSettingsSection; if (null != clientSettingsSection) { // Try to get existing entry System.Configuration.SettingElement settingElement = clientSettingsSection.Settings.Get(key); if (null != settingElement) { // Remove existing entry clientSettingsSection.Settings.Remove(settingElement); // Set new value settingElement.Value.ValueXml.InnerXml = value; clientSettingsSection.Settings.Add(settingElement); } else { // Create new entry settingElement = new System.Configuration.SettingElement(key, System.Configuration.SettingsSerializeAs.String); settingElement.Value = new System.Configuration.SettingValueElement(); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); settingElement.Value.ValueXml = doc.CreateElement("value"); // Set new value settingElement.Value.ValueXml.InnerXml = value; clientSettingsSection.Settings.Add(settingElement); } // Save changes config.Save(this.ConfigurationSaveMode); System.Configuration.ConfigurationManager.RefreshSection("userSettings"); } } catch (Exception ex) { HandleException(ex); } return; } #endregion #region TheAppName.Properties.Settings | applicationSettings if (writeInto.Equals("applicationSettings")) { try { //<applicationSettings> // <TheAppName.Properties.Settings> // <setting name="TheKey" serializeAs="String"> // <value>TheValue</value> // </setting> // </TheAppName.Properties.Settings> //</applicationSettings> #region Get Configuration instance System.Configuration.Configuration config = null; if (null != ConfigurationInstanceProvider) { config = ConfigurationInstanceProvider(); } else { config = System.Configuration.ConfigurationManager.OpenExeConfiguration(this.ConfigurationUserLevel); } #endregion System.Configuration.ClientSettingsSection clientSettingsSection = config.GetSection("applicationSettings/" + this.AppName + ".Properties.Settings") as System.Configuration.ClientSettingsSection; if (null != clientSettingsSection) { // Try to get existing entry System.Configuration.SettingElement settingElement = clientSettingsSection.Settings.Get(key); if (null != settingElement) { // Remove existing entry clientSettingsSection.Settings.Remove(settingElement); // Set new value settingElement.Value.ValueXml.InnerXml = value; clientSettingsSection.Settings.Add(settingElement); } else { // Create new entry settingElement = new System.Configuration.SettingElement(key, System.Configuration.SettingsSerializeAs.String); settingElement.Value = new System.Configuration.SettingValueElement(); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); settingElement.Value.ValueXml = doc.CreateElement("value"); // Set new value settingElement.Value.ValueXml.InnerXml = value; clientSettingsSection.Settings.Add(settingElement); } // Save changes config.Save(this.ConfigurationSaveMode); System.Configuration.ConfigurationManager.RefreshSection("applicationSettings"); } } catch (Exception ex) { HandleException(ex); } return; } #endregion #region appSettings //if (writeInto.Equals("appSettings")) { //<appSettings> // <add key="TheKey" value="TheValue" /> //</appSettings> // Does not save the new value into app.config file (only changed for app runtime): //System.Configuration.ConfigurationManager.AppSettings[key] = value; #region Get Configuration instance System.Configuration.Configuration config = null; if (null != ConfigurationInstanceProvider) { config = ConfigurationInstanceProvider(); } else { config = System.Configuration.ConfigurationManager.OpenExeConfiguration(this.ConfigurationUserLevel); } #endregion if (null == config.AppSettings.Settings[key]) { config.AppSettings.Settings.Add(key, value); } else { config.AppSettings.Settings[key].Value = value; } config.Save(this.ConfigurationSaveMode); System.Configuration.ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name); //return; } #endregion }
/// <summary> /// Gets the value for the specified key from the persistent storage (app.config, etc). /// <locDE><para />Holt den Wert für den angegebenen Schlüsselbegriff aus dem Konfigurationsspeicher (app.config, etc).</locDE> /// </summary> /// <param name="key">The key.<locDE><para />Der Schlüsselbegriff.</locDE></param> /// <param name="foundAt">The found at identifier, i.e. userSettings, applicationSettings or appSettings. /// <locDE><para />Die Kennung der Fundstelle, z.B. userSettings, applicationSettings or appSettings.</locDE></param> /// <param name="defaultValue">The default value.<locDE><para />Der Vorgabewert.</locDE></param> /// <returns> /// Loaded value or default value if not available.<locDE><para />Geladener Wert oder Vorgabewert falls nicht vorhanden.</locDE> /// </returns> public string GetValue(string key, out string foundAt, string defaultValue = "") { foundAt = null; if (string.IsNullOrWhiteSpace(key)) { return(defaultValue); } #region TheAppName.Properties.Settings | userSettings string value = null; try { //<userSettings> // <TheAppName.Properties.Settings> // <setting name="TheKey" serializeAs="String"> // <value>TheValue</value> // </setting> // </TheAppName.Properties.Settings> //</userSettings> #region Get Configuration instance System.Configuration.Configuration config = null; if (null != ConfigurationInstanceProvider) { config = ConfigurationInstanceProvider(); } else { config = System.Configuration.ConfigurationManager.OpenExeConfiguration(this.ConfigurationUserLevel); } #endregion System.Configuration.ClientSettingsSection clientSettingsSection = config.GetSection("userSettings/" + this.AppName + ".Properties.Settings") as System.Configuration.ClientSettingsSection; if (null != clientSettingsSection) { // Try to get existing entry System.Configuration.SettingElement settingElement = clientSettingsSection.Settings.Get(key); if (null != settingElement) { value = (settingElement.Value.ValueXml).LastChild.InnerText.ToString(); foundAt = "userSettings"; if (string.IsNullOrEmpty(value)) { return(defaultValue); } return(value); } } } catch (Exception ex) { HandleException(ex); } #endregion #region TheAppName.Properties.Settings | applicationSettings try { //<applicationSettings> // <TheAppName.Properties.Settings> // <setting name="TheKey" serializeAs="String"> // <value>TheValue</value> // </setting> // </TheAppName.Properties.Settings> //</applicationSettings> #region Get Configuration instance System.Configuration.Configuration config = null; if (null != ConfigurationInstanceProvider) { config = ConfigurationInstanceProvider(); } else { config = System.Configuration.ConfigurationManager.OpenExeConfiguration(this.ConfigurationUserLevel); } #endregion System.Configuration.ClientSettingsSection clientSettingsSection = config.GetSection("applicationSettings/" + this.AppName + ".Properties.Settings") as System.Configuration.ClientSettingsSection; if (null != clientSettingsSection) { // Try to get existing entry System.Configuration.SettingElement settingElement = clientSettingsSection.Settings.Get(key); if (null != settingElement) { value = (settingElement.Value.ValueXml).LastChild.InnerText.ToString(); foundAt = "applicationSettings"; if (string.IsNullOrEmpty(value)) { return(defaultValue); } return(value); } } } catch (Exception ex) { HandleException(ex); } #endregion #region appSettings try { //<appSettings> // <add key="TheKey" value="TheValue" /> //</appSettings> value = System.Configuration.ConfigurationManager.AppSettings[key]; } catch { } foundAt = "appSettings"; if (string.IsNullOrEmpty(value)) { return(defaultValue); } return(value); #endregion }