Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / Windows Forms

Making Application Settings Portable

Rate me:
Please Sign up or sign in to vote.
4.15/5 (17 votes)
9 Feb 2019CPOL4 min read 42.1K   1K   37   27
How to create a custom settings provider for .NET applications

Introduction

This article shows how to store .NET application settings in the application's folder (or any other place) to allow them to be portable together with the app. This is achieved by creating a custom settings provider which handles loading and storing of settings.

An updated version of this implementation with some little additions is hosted on GitHub at https://github.com/bluegrams/SettingsProviders. Another settings provider which attempts to store settings in JSON format can also be found there.

Background

.NET provides an integrated mechanism for storing application and user settings which makes it easy for developers to create and use these settings. To give a brief overview: Settings for Windows Forms (or WPF) generally are divided into two categories: Read- and writeable user-scoped settings stored for every user account and application-scoped which are typically read-only at runtime. Both types are accessed through a settings class derived from ApplicationSettingsBase which provides the settings properties and basic methods for loading and saving.

The actual data however is provided by a class derived from SettingsProvider which handles loading and storing the data. The default settings provider stores user-scoped settings in an XML-like format in the file user.config in the AppData folder of the user. This is quite sufficient for a typical application, but in some scenarios (e.g. a portable application that should not touch the AppData folder), an alternative storage location would be preferable. As the storage location of the default settings provider can't be changed, we must implement our own settings provider. This is not quite complicated as just a few methods have to be implemented. Nevertheless, there are a few tricky points to be considered.

Using the Code

The Standard Way

Applying the portable settings provider to your application settings can simply be done within Visual Studio. To do this, go to the settings designer, select a setting and open its properties page. It contains a Provider field which is empty by default, set this to the name of the provider. Note that you must specify the full name of the class including namespaces, which is Bluegrams.Application.PortableSettingsProvider in this case.

Image 1

It is also noticeable that the settings provider has to be set individually for every settings property on this way. This allows flexibility but it may not be the easiest method especially for large settings files.

The Quick Way

Therefore, this implementation of a portable settings provider provides a simpler method. To make all settings in a settings class portable, just use its ApplyProvider() method like this:

C#
// make the default settings class portable
PortableSettingsProvider.ApplyProvider(Properties.Settings.Default);

The Config File

When applied, the portable settings provider stores the user-scoped settings in a file named portable.config in the application folder. If this file is not existent, the default values are used and a new file is created. The data is serialized in an XML-style format similar to the default settings files:

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <userSettings>
    <Roaming>
      <Bluegrams.Application.WPF.Properties.Settings>
        <Culture></Culture>
      </Bluegrams.Application.WPF.Properties.Settings>
      <TestWpfApp.Properties.Settings>
        <RoamedSetting>Default text</RoamedSetting>
      </TestWpfApp.Properties.Settings>
    </Roaming>
    <PC_MachineName>
      <Bluegrams.Application.WPF.Properties.Settings>
        <Width>640</Width>
        <Height>480</Height>
        <Left>100</Left>
        <Top>100</Top>
      </Bluegrams.Application.WPF.Properties.Settings>
      <TestWpfApp.Properties.Settings>
        <LocalSetting>Default text</LocalSetting>
      </TestWpfApp.Properties.Settings>
    </PC_MachineName>
  </userSettings>
</configuration>

Several points are noticeable. First, the file is divided in a <Roaming> section and a section with the name of the current PC. Properties in the roaming section are the ones which are portable to another machine, whereas all other settings are specific to one machine (settings like window location or size should be found here). To place a setting in the roaming section, just set the Roaming property to true for that setting (see image above).

The settings properties then are further divided into the settings classes they belong to. This allows different classes from different locations to share the same settings file.

Points of Interest

A custom settings provider must only implement three methods, where GetPropertyValues() and SetPropertyValues() are the two important ones. These methods receive a collection of settings properties and perform loading or saving of the XML file. As the provider should only handle user-scoped settings and it must differentiate between roamed and non-roamed settings, the methods IsRoaming() and IsUserScoped() iterate through a property's attributes to check this. The IsRoaming() method searches for the SettingsManageabilityAttribute whereas the IsUserScoped() method indicates whether the UserScopedSettingAttribute is present.

As a third method, Initialize() must be overridden from the base class. Here, it is important to specify a value for the name argument as this argument cannot be left empty.

Next, we make sure the Reload() method works as expected. To achieve this, the settings provider does not cache any loaded XML content on its own but loads the settings from file every time the GetPropertyValues() method is called. This sounds extremely inefficient but the settings class caches the properties itself, so the settings provider only comes into action if the settings really have to be reloaded because, for example, the Reload() method is called.

As a last point of interest, have a look at the Reset() and Upgrade() methods available in every settings class. To provide support for this methods, the settings provider has to implement an interface called IApplicationSettingsProvider. In this implementation, the Reset() method simply deletes the settings file to reset all settings to their default values.

History

  • 2018-11-17: Fixed several bugs in the implementation
  • 2018-04-08: Initial version
This article was originally posted at https://github.com/bluegrams/MiniAppManager

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNeed help using with dll Pin
RafStudio6-Dec-21 21:49
RafStudio6-Dec-21 21:49 
QuestionProject specific settings file Pin
Steven Ackerman21-May-20 7:57
Steven Ackerman21-May-20 7:57 
AnswerRe: Project specific settings file Pin
Steven Ackerman21-May-20 10:04
Steven Ackerman21-May-20 10:04 
QuestionWhere do I put the settings Pin
Member 1474620923-Feb-20 3:54
Member 1474620923-Feb-20 3:54 
AnswerRe: Where do I put the settings Pin
Member 1474620923-Feb-20 4:05
Member 1474620923-Feb-20 4:05 
GeneralRe: Where do I put the settings Pin
Member 1474620923-Feb-20 4:34
Member 1474620923-Feb-20 4:34 
GeneralRe: Where do I put the settings Pin
Member 1474620923-Feb-20 4:54
Member 1474620923-Feb-20 4:54 
GeneralRe: Where do I put the settings Pin
Quanik23-Feb-20 9:01
Quanik23-Feb-20 9:01 
GeneralRe: Where do I put the settings Pin
Member 1474620923-Feb-20 10:24
Member 1474620923-Feb-20 10:24 
GeneralRe: Where do I put the settings Pin
Member 1474620924-Feb-20 17:12
Member 1474620924-Feb-20 17:12 
AnswerMessage Closed Pin
23-Feb-20 8:24
Quanik23-Feb-20 8:24 
QuestionPersistant? Pin
Member 1073468517-Jan-20 13:04
Member 1073468517-Jan-20 13:04 
AnswerRe: Persistant? Pin
Quanik18-Jan-20 22:01
Quanik18-Jan-20 22:01 
QuestionIs it compatible with WPF? Pin
Member 1326444323-Oct-19 3:50
Member 1326444323-Oct-19 3:50 
AnswerRe: Is it compatible with WPF? Pin
Quanik26-Oct-19 23:54
Quanik26-Oct-19 23:54 
GeneralRe: Is it compatible with WPF? Pin
Member 1326444327-Oct-19 2:02
Member 1326444327-Oct-19 2:02 
GeneralMy vote of 5 Pin
Jeff Bowman27-Jul-19 9:40
professionalJeff Bowman27-Jul-19 9:40 
QuestionSmall Difference Pin
Ixus926-Aug-18 22:35
Ixus926-Aug-18 22:35 
AnswerRe: Small Difference Pin
Quanik31-Aug-18 5:05
Quanik31-Aug-18 5:05 
GeneralRe: Small Difference Pin
Ixus96-Sep-18 19:39
Ixus96-Sep-18 19:39 
GeneralRe: Small Difference Pin
Quanik7-Sep-18 2:31
Quanik7-Sep-18 2:31 
GeneralMy vote of 5 Pin
Thomas Schittli16-Apr-18 2:13
Thomas Schittli16-Apr-18 2:13 
GeneralRe: My vote of 5 Pin
Quanik19-Jan-20 6:28
Quanik19-Jan-20 6:28 
NewsNuget package Pin
Quanik13-Apr-18 5:54
Quanik13-Apr-18 5:54 
Questionnot implement inherited abstract member error Pin
Joel WZ9-Apr-18 7:51
professionalJoel WZ9-Apr-18 7:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.