How to Store User Settings for a .Net Application

What is the best way to store user settings for a .NET application?

I love using the built-in Application Settings. Then you have built in support for using the settings designer if you want at design-time, or at runtime to use:

// read setting
string setting1 = (string)Settings.Default["MySetting1"];
// save setting
Settings.Default["MySetting2"] = "My Setting Value";

// you can force a save with
Properties.Settings.Default.Save();

It does store the settings in a similar folder structure as you describe (with the version in the path). However, with a simple call to:

Properties.Settings.Default.Upgrade(); 

The app will pull all previous versions settings in to save in.

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

Store User Settings in ASP.NET Core Identity AspNetUsers Table or Not

Always a separate table/class.

Complexity grows with the application. The table will become too large then you would spend time moving away from this. We have similar in our legacy application with an Organisation that has grown to 70+ columns. I've heard stories on Twitter about people with similar experiences.

Also alongside this you can think about Single Responsibility. Given a single reason for change - or more to Uncle Bobs updated definition "A module should be responsible to one, and only one, actor".

You could end up having settings per module - and the more you add, this is just going to get bigger. Sure, just leave it till later to change things - but this never happens.

For me the Identity is its own system. Not your system. It provides a mechanism to authenticate. It uses most columns in the able or fields in the user class to do this.

Your settings is about your application - this is your domain. If you replace Identity, you might replace that user table/user class. Then where will your settings end up?

Store user settings into application folder

You can control the location of the user.config file by creating a custom SettingsProvider. Luckily for you, someone at CodeProject already did that.

See my answer here for all the details: How to make designer generated .Net application settings portable

Best way for store a web application settings and options

Store it in the database. That part is easy. If you're concerned about querying the database too much, then cache the results of your query for some time period. At its most simplest, that would look something like:

ObjectCache cache = MemoryCache.Default;

var key = "MyAwesomeSettings";
var settings = cache.Get(key);
if (settings == null)
{
settings = // query settings;
cache.Add(key, settings, DateTimeOffset.UtcNow.AddHours(1));
}

However, one key issue with MemoryCache is that it's process-bound. If you're using web workers, you're going to have a unique cache per worker, as each worker is a separate process. You can use a NoSQL solution like Redis, though, to create a distributed cache that can be shared between workers and is more resilient to boot.

Where to store .NET application settings


I want the admin users to be able to modify those values periodically via an admin web page

Database, definitely. The config file is a convenient place to store non-static values, but not always the best place. Step back for a moment and consider two "buckets" of such values:

  1. Logical configurations which users can change within any given instance of the application.
  2. Infrastructure configurations which define any given instance of the application and how it relates to the surrounding environment.

The former is best kept in the application's database, the latter in the config file. The former might contain values like:

  • Business holidays observed this year
  • Values used in calculating business data
  • Defaults for user forms

While the latter might contain values like:

  • Database connection strings
  • URLs for external web services
  • Credentials for service accounts

Let the users change things that they can change without physically breaking the application, and track those changes in the database. Keep the infrastructure details in the config file, since a change to those details would very likely require an app pool restart anyway.

How can I save application settings in a Windows Forms application?

If you work with Visual Studio then it is pretty easy to get persistable settings. Right click on the project in Solution Explorer and choose Properties. Select the Settings tab and click on the hyperlink if settings doesn't exist.

Use the Settings tab to create application settings. Visual Studio creates the files Settings.settings and Settings.Designer.settings that contain the singleton class Settings inherited from ApplicationSettingsBase. You can access this class from your code to read/write application settings:

Properties.Settings.Default["SomeProperty"] = "Some Value";
Properties.Settings.Default.Save(); // Saves settings in application configuration file

This technique is applicable both for console, Windows Forms, and other project types.

Note that you need to set the scope property of your settings. If you select Application scope then Settings.Default.<your property> will be read-only.

Reference: How To: Write User Settings at Run Time with C# - Microsoft Docs

Where to save .Net MAUI user settings

It all comes down to your requirements really. There is no silver bullet to this.

I must admit I didn't really know the settings (designer) file that you mentioned. I can tell that that's not a thing (as far as I know) in Xamarin or .NET MAUI. Probably also because there is the concept of a sandbox in mobile apps that is less a thing on desktop apps. So the location where things are saved are a bit more important.

In the comments there are already a couple of great suggestions to get you started. If your requirement is to just save simple types (string, bool, int, etc.) then the Essentials Preferences API should work just fine. I think under the hood that just serializes to a json file and stores it somewhere. Essentials is indeed already part of .NET MAUI at this time.

Xamarin.Forms used to have their own Properties, which is I think a more common concept in .NET applications, but I think the Essentials way is better.

If you want to save more complex things you might want to consider using a local database like SQLite or just roll your own and serialize your settings and save that somewhere, preferably with the Essentials FileSystem Helpers.

One thing to note here, and that is where the value of these libraries come in, is where you save the settings. Do you want the settings to be backed up to a cloud service so if survives a reinstall of the app? Or even an install of the same app on a new phone? Or do you then just want to reset the whole state and don't care about it.

If you figure that out, the rest is up to you! Hope this helps.

Save Settings in a .NET Winforms Application

At some point, the answer boils down to a matter of taste. I'd say you'll end up with at least these options:

  • store it in the registry, where you have the HKEY_CURRENT_USER key. Everything under it is user-specific. This is usually preferred when you want to store a bunch of small key-value pairs. The main advantage of this system is that your settings are easy to find and share throughout several different applications. To follow this path, you can start from here.
  • using .NET Application settings, provides the easiest way to access your settings at runtime. Again, it's better for using with key-value pairs of small-sized data. IMO, the main advantages of this method is its simplicity and the fact that it empowers you to use some .NET classes as values (not forcing you to convert everything into more basic types). To follow this path, you can start from here.
  • store it in User Data folders, which are usually hidden under the user's profile directory. This is preferred when you want to store a large amount of data or any number of files. The main advantage of this method is that you can manipulate your data as you would with any files (that may also be a disadvantage). To follow this path, you can start from here.


Related Topics



Leave a reply



Submit