Where should I store settings of my application?

By Mirek on (tags: c#, settings, WPF, categories: architecture)

Where should I store user and application settings in my WPF application? Well, in Settings of course.

Go to your WPF application properties and Settings tab. If there is nothing there but text in the middle, click on that text. This will create the Settings file in your project.
Here you can add settings in the grid. The columns are: Name, Type, Scope and the Value. Name and type are self explanatory. What is important the Name is strongly type so you will be able to access a property with that name while coding. In Type column you have to select one of many available types. The scope of the settings can be User or Application. Application scope settings are read-only and stored in app.config of your app. That means whatever you type in Settings designer is later available in your application during runtime, but nothing can be changed from the code. The only modification to application settings can be done manually by editing config file of the application.

User settings are saved in user.config for current system user and can be edited in runtime from the application.

We can read and write user settings simply

We had here UserName of type string, UserNumber of type double and Password of type string. Notice line number 17, where the modified user settings are saved.

Application settings are accessed in the same way. However any try to assign a new value to application setting will throw compilation error, because application settings are a read-only properties.

You may wonder why is it “Properties.Settings.Default” and what can be other then Default at the end. Well we can easily check that by going to the definition of “Default” which leads us to the auto generated settings class

As you can see this is just a reference to the singleton instance of settings class. It can differ when we would use our own set of settings. Yes we can have many settings groups in our application. Just add new item and select Settings file. This allows grouping and managing settings as well as improving performance when modifying only a subset of settings.

For instance having

we can then save “MySettings”

Which causes only those settings being persisted. No other settings are persisted.

That’s it. Now every system user which runs your application on the same computer can has his own settings and preferences. More details on MSDN.