Articles in categories

Articles

Read-Write properties to settings.settings file

For an application I made I needed to store some properties in the app.config file. The user can change these values through a configuration screen. This worked fine untill I deployed the application with ClickOnce.

With every new build I made the user's values were overwritten with the default values in the config, which makes sence. So I started to search for a solution that would keep the user's settings. I found this by using the settings file in the solution and a small method that copies the usersettings from the old application version to the new application on start up.

You can find the Settings by rightclicking your project in in the solution explorer and then select 'Properties'.
settings.jpg

They will show up in your app.config file under the section <userSettings>

config usersettings

How to read - write to them?

Read:

 public static string ReadUserSetting(string key)
 {
   return Settings.Default[key].ToString();
 }

Write:

public static void SetUsersetting(string key, string value)
{
   Settings.Default[key] = value;
   Settings.Default.Save();
}

Copy the settings from the old application version to the new version (run this method when your application starts. The var 'upgradeRequired' is also a setting.):

public static void UpgradeUserSettings()
{
  if (Settings.Default.upgradeRequired)
  {
    Settings.Default.Upgrade();
    Settings.Default.upgradeRequired = false;
    Settings.Default.Save();
  }
} 

Complete ConfigurationHelper.cs (also available as a download below)

using System;
using System.Configuration;
using TestApp.Properties;

namespace Stieven.Helpers
{
  public static class ConfigurationHelper
  {
    #region AppSettingsKeys
    /// <summary>
    /// Reads all keys from appsettings section in config file
    /// </summary>
    public static void ReadAllKeys()
    {
      foreach (var key in ConfigurationManager.AppSettings.AllKeys)
      {
        string value = ConfigurationManager.AppSettings.Get(key);
        Console.WriteLine("Key: {0} || Value: {1}", key, value);
      }
    }

    /// <summary>
    /// Add new key to appsettings section in config file
    /// </summary>
    public static void AddNewConfigsetting(string key, string value)
    {
      Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      conf.AppSettings.Settings.Add(key, value);
      conf.Save(ConfigurationSaveMode.Modified);
      ConfigurationManager.RefreshSection("appSettings");
    }

    /// <summary>
    /// Updates a key in appsettings section in config file
    /// </summary>
    public static void UpdateConfigsetting(string key, string value)
    {
      Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      conf.AppSettings.Settings.Remove(key);
      conf.AppSettings.Settings.Add(key, value);
      conf.Save(ConfigurationSaveMode.Modified);
      ConfigurationManager.RefreshSection("appSettings");
    }

    /// <summary>
    /// Reads a specific key from appsettings section in config file
    /// </summary>
    public static string ReadAppSetting(string key)
    {
      return ConfigurationManager.AppSettings.Get(key);
    } 
    #endregion

    #region UserSettingsKeys
    /// <summary>
    /// Read UserSetting from settings file
    /// </summary>
    public static string ReadUserSetting(string key)
    {
      return Settings.Default[key].ToString();
    }

    /// <summary>
    /// Sets a UserSetting in the settings file (string)
    /// </summary>
    public static void SetUsersetting(string key, string value)
    {
      Settings.Default[key] = value;
      Settings.Default.Save();
    }

    /// <summary>
    /// Sets a UserSetting in the settings file (int)
    /// </summary>
    public static void SetUsersetting(string key, int value)
    {
      Settings.Default[key] = value;
      Settings.Default.Save();
    }

    /// <summary>
    /// Upgrades the UserSettings during ClickOnce so that the user defined settings are not overwritten 
    /// with the default deployment settings
    /// </summary>
    public static void UpgradeUserSettings()
    {
      if (Settings.Default.upgradeRequired)
      {
        Settings.Default.Upgrade();
        Settings.Default.upgradeRequired = false;
        Settings.Default.Save();
      }
    } 
    #endregion
  }
}

You can download the full ConfigurationHelper.cs file with the download link below. This will also include the content of article Read-Write to config