User application settings using ApplicationSettingsBase

There are many ways to store application settings, whether they’re roll your own XML or old INI profile file settings. Another option is to use a class derived from ApplicationSettingsBase.

ApplicationSettingsBase

To create your own configuration class, you derive it from the ApplicationSettingsBase class and then add properties to define the values you want to persist. On top of this we can override methods such as Save to allow us to customise the saving of values etc. A simple example is listed below (note: we’d often write this class as a singleton but I’ve removed extraneous code from the sample).

public sealed ProxySettings : ApplicationSettingsBase
{
   [UserScopedSetting]
   public string Host
   {
      get { return (string)this["Host"]; }
      set { this["Host"] = value; }
   }

   [UserScopedSetting, DefaultSettingValue("8080")]
   public int Port
   {
      get { return (int)this["Port"]; }
      set { this["Port"] = value; }
   }

   [ApplicationScopedSetting]
   public string ApplicationContext
   {
      get { return (string)this["ApplicationContext"]; }
   }
}

For a property to be stored or read using this mechanism, it must have the UserScopedSetting or the ApplicationScopedSettings attribute applied to it, otherwise it’s ignored from the perspective of persistence but obviously you can still use it as a non-persisted property.

ApplicationScopedSettings is used for application specific settings whereas UserScopedSetting is used for user specific settings (I know the names give this away but I thought I should make it clear). In the case of UserScopedSettings these, by default, are stored in the Documents and Settings\\Application Data in a user.config file whereas ApplicationScoreSettings are stored within the app.config for the application.

Note: The user scoped settings are not stored automatically, you need to call the ApplicationSettingsBase.Save method.

ApplicationScopedSettings are in essence read-only. Whilst we can have a getter and setter on the class (above), allowing us to change the application setting at run-time the altered values are not persisted to a storage mechanism.

Q. So what’s the point of ApplicationScopedSetting ?

A. You can add these values to your app.config and use it from your project in a similar way to using ConfigurationManager.AppSettings but obviously it can reside along with your user settings in a class as above and thus both the user and application scoped values are read in the same way, using this class. Underneath they’re separated into the user.config and app.config.

You can declare a default value to an ApplicationScopedSetting and then if you wanted to change the value in the app.config you’ll have a fallback default if the configuration is missing. The app.config will look something like

<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="MyApplication.ConfigurationSettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <MyApplication.ConfigurationSettings>
      <setting name="ProxyAdddress" serializeAs="String">
        <value>BlahBlah</value>
      </setting>
    </MyApplication.ConfigurationSettings>
  </applicationSettings>
</configuration>

Creating your application settings via a designer

Creating your ApplicationSettingsBase derived class is extremely useful if you have some more complex code (other than just read/write). But you can also create your own settings within Visual Studio and have it automatically created the source code for you. Simply go to the Properties folder in your solution and use the Settings.settings file, double clicking on this allows you to edit your properties and the Designer.cs will automatically be generated as a singleton and creates all the stuff as we’ve done previously ourselves. Better still this will generated the app.config code as well and save us having to remember the correct configuration xml.

You can ofcourse add more .settings files using the Add | New Item and select the Settings File.

Note: If you’re trying to track down where your configuration files might have been saved you can always call

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Console.WriteLine(configuration.FilePath);
configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
Console.WriteLine(configuration.FilePath);
configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
Console.WriteLine(configuration.FilePath);