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

   1: Console.WriteLine("UserName: {0}", Properties.Settings.Default.UserName);
   2: Console.WriteLine("UserNumber: {0}", Properties.Settings.Default.UserNumber);
   3: Console.WriteLine("Password: {0}", Unprotect(Properties.Settings.Default.Password));
   4:  
   5:  
   6: Console.WriteLine("Provide new values for...");
   7: Console.Write("UserName: ");
   8: Properties.Settings.Default.UserName = Console.ReadLine();
   9: Console.Write("UserNumber: ");
  10: double aux = 0.0;
  11: double.TryParse(Console.ReadLine(), out aux);
  12: Properties.Settings.Default.UserNumber = aux;
  13:  
  14: Console.Write("Password: ");
  15: Properties.Settings.Default.Password = Console.ReadLine();
  16:  
  17: Properties.Settings.Default.Save();
  18: Console.WriteLine("User settings saved... Press any key to quit.");
  19: Console.ReadLine();

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

   1: public sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
   2:  
   3: private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
   4:  
   5: public static Settings Default 
   6: {
   7:     get 
   8:     {
   9:         return defaultInstance;
  10:     }
  11: }

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

   1: Properties.MySettings.Default.MySetting = "My special setting value"

we can then save “MySettings”

   1: Properties.MySettings.Default.Save();

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.