C# - Approach for Saving User Settings in a Wpf Application

C# - approach for saving user settings in a WPF application?

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

Equivalent to UserSettings / ApplicationSettings in WPF .NET 5, .NET 6 or .Net Core

Sample Image

You can add the same old good settings file e.g. via the right click on the Properties -> Add -> New Item and search for the "Settings". The file can be edited in the settings designer and used as in the .net framework projects before (ConfigurationManager, Settings.Default.Upgrade(), Settings.Default.Save, etc. works).

Add also the app.config file to the project root folder (the same way via the Add -> New Item), save the settings once again, compile the project and you will find a .dll.config file in the output folder. You can change now default app values as before.

Tested with Visual Studio 1.16.3.5 and a .net core 3.0 WPF project.

WPF Saving UI settings to .txt file

This question will most likely be closed for being opinion-based, but I would recommend saving your control states to the built-in WPF User Settings file that comes with your project, instead of creating your own. There's nothing wrong with creating your own, but why build your own parser when Visual Studio has already provided one for you. You will find this file under your project's Properties->Settings.settings file. This file gets automatically loaded when ever the user launches your WPF app. This file is stored in your %LocalAppData% folder, under the appropriate Company name and Product name, if you specified one.

Let's say the Settings.settings file contains the following states that you want to save:

Sample Image

Then in your code, find the appropriate time to load these settings into your app's variables:

// Load 
string lastUser = Properties.Settings.Default.LastUser;
bool detailsEnabled = Properties.Settings.Default.ShowDetails;
int timeout = Properties.Settings.Default.FormTimeoutSeconds;

And when you want to save the control states, such as when the user closes your app, or changes a state value, do this:

// Save settings
Properties.Settings.Default.LastUser = lastUser;
Properties.Settings.Default.ShowDetails = detailsEnabled;
Properties.Settings.Default.FormTimeoutSeconds = timeout;
Properties.Settings.Default.Save();

How to save user settings programmatically?

Set

Properties.Settings.Default.myColor = Color.AliceBlue;

Get

this.BackColor = Properties.Settings.Default.myColor;

Save

If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:

Properties.Settings.Default.Save();

Reference

C# WPF Application save settings in UserControl

It looks like you are just assigning the properties and not saving them. You need to call the Save method

Vorschau.Properties.Settings.Default.Save();

You can find the file under %userprofile%\appdata\local after saving. See here for details.

For this to work, the scope of the setting should be "User".

WPF Properties.Settings saving and multiple users

User scoped settings are just that, settings that an individual user can change and will only be saved for that user.

The application scoped settings will affect all users but they are not designed to be changed by a user.

You might want to consider a different approach to storing settings that you want users to be able to change but to affect all users of an application e.g. the Windows registry or an external xml file.

Another option is to use user scoped settings but to change the location to a centralised location so that all users use/save the same settings. See Store user settings into application folder for an option on how to do this.

How to dynamicaly load and save user settings in WPF multi window application?

Well, in fact, this works only on execution mode compiled in release (in my case).

Where to place application settings for a WPF application

You could for example use Application.Current.Properties: https://msdn.microsoft.com/en-us/library/aa348545(v=vs.100).aspx

Or you could use the Settings class if you require the settings to be edited during runtime and saved across runs: https://blogs.msdn.microsoft.com/patrickdanino/2008/07/23/user-settings-in-wpf/



Related Topics



Leave a reply



Submit