What approach do you recommend for persisting user settings in a WPF windows (desktop) application? Note that the idea is that the user can change their settings at run time, and then can close down the application, then when starting up the application later the application will use the current settings. Effectively then it will appear as if the application settings do not change.
Q1 - Database or other approach? I do have a sqlite database that I will be using anyway hence using a table in the database would be as good as any approach?
Q2 - If Database: What database table design? One table with columns for different data types that one might have (e.g. string
, long
, DateTime
etc) OR just a table with a string for the value upon which you have to serialize and de-serialize the values? I'm thinking the first would be easier, and if there aren't many settings the overhead isn't much?
Q3 - Could Application Settings be used for this? If so are there any special tasks required to enable the persistence here? Also what would happen regarding usage of the "default" value in the Application Settings designer in this case? Would the default override any settings that were saved between running the application? (or would you need to NOT use the default value)
You can store your settings info as
Strings
of XML in theSettings.Default
. Create some classes to store your configuration data and make sure they are[Serializable]
. Then, with the following helpers, you can serialize instances of these objects--orList<T>
(or arraysT[]
, etc.) of them--toString
. Store each of these various strings in its own respectiveSettings.Default
slot in your WPF application'sSettings
.To recover the objects the next time the app starts, read the
Settings
string of interest andDeserialize
to the expected typeT
(which this time must be explcitly specified as a type argument toDeserialize<T>
).You can use Application Settings for this, using database is not the best option considering the time consumed to read and write the settings(specially if you use web services).
Here are few links which explains how to achieve this and use them in WPF -
User Settings in WPF
Quick WPF Tip: How to bind to WPF application resources and settings?
A Configurable Window for WPF
I wanted to use an xml control file based on a class for my VB.net desktop WPF application. The above code to do this all in one is excellent and set me in the right direction. In case anyone is searching for a VB.net solution here is the class I built:
I typically do this sort of thing by defining a custom [
Serializable
] settings class and simply serializing it to disk. In your case you could just as easily store it as a string blob in your SQLite database.In all the places I've worked, database has been mandatory because of application support. As Adam said, the user might not be at his desk or the machine might be off, or you might want to quickly change someone's configuration or assign a new-joiner a default (or team member's) config.
If the settings are likely to grow as new versions of the application are released, you might want to store the data as blobs which can then be deserialized by the application. This is especially useful if you use something like Prism which discovers modules, as you can't know what settings a module will return. The blobs could be keyed by username/machine composite key. That way you can have different settings for every machine.
I've not used the in-built Settings class much so I'll abstain from commenting. :)
I also prefer to go with serialization to file. XML files fits mostly all requirements. You can use the
ApplicationSettings
build in but those have some restrictions and a defined but (for me) very strange behavior where they stored. I used them a lot and they work. But if you want to have full control how and where they stored I use another approach.MySettings
Advantages:
Disadvantages: - You have to think about where to store your settings files. (But you can just use your installation folder)
Here is a simple example (not tested)-
And here is how to use it. It's possible to load default values or override them with the user's settings by just checking if user settings exist:
maybe someone get's inspired by this approach. This is how I do it now for many years and I'm quite happy with that.