Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App.config: User vs Application Scope

I have added App.config file in my project. I have created two settings from Project > Properties > Settings panel -

enter image description here

I have noticed that when I am adding a setting, I can define scope as User or Application. -

  1. User
  2. Application

If I define setting as User it goes touserSettings section,
if I define setting as Application it goes to applicationSettings section

App.config

<configuration>      <userSettings>         <DemoApp.Properties.Settings>             <setting name="MySetting1" serializeAs="String">                 <value>Value1</value>             </setting>         </DemoApp.Properties.Settings>     </userSettings>      <applicationSettings>         <DemoApp.Properties.Settings>             <setting name="MySetting2" serializeAs="String">                 <value>Value2</value>             </setting>         </DemoApp.Properties.Settings>     </applicationSettings>  </configuration> 

But, these settings can be accessed in the same way from .cs -

Code

string mySetting1 = DemoApp.Properties.Settings.Default.MySetting1; string mySetting2 = DemoApp.Properties.Settings.Default.MySetting2; 

What is the difference between User and Application scope and under what circumstances one should choose between these two?

like image 605
Parag Meshram Avatar asked Oct 24 '12 10:10

Parag Meshram


People also ask

When should I use app config?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.

Do I need app config?

config are only required, if you have coded your application in such a way that it is explicitly dependent on it. If you have not done this, or have put error handling/default values or actions in place where it can't read the config file, one would assume your application could run without it.

Where are .NET application settings stored?

It is in a folder with your application's name in Application Data folder in User's home folder (C:\documents and settings\user on xp and c:\users\user on Windows Vista).

What are application settings?

Application settings enable you to store application information dynamically. Settings allow you to store information on the client computer that shouldn't be included in the application code (for example a connection string), user preferences, and other information you need at runtime.


2 Answers

Basically, application settings cannot be changed during the running of a program and user settings can. These user settings should then be saved so the user is presented with a familiar experience when (s)he runs the application next.

Edit: For examples, you might write your application with different modules, and need to ensure that your main module is using the correct version of your security module. For this you would set up an application-scope setting eg:

SecurityModuleVersion  string     Application      v1.21 

Sometime later when you refactor the security module, you might change the value to v1.22 when you deploy to ensure the correct security is being implemented

On the other hand, if your application has different 'skins' with color changes, font changes etc, then you may setup a user setting something like the following:

ApplicationSkin        string     User              DefaultSkin 

Then, when Michelle changes to the skin she prefers, the application remembers her settings. The properties may now look like:

ApplicationSkin        string     User              HelloKittySkin 
like image 181
mcalex Avatar answered Oct 12 '22 22:10

mcalex


Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions. User-scope settings, however, can be written at run time, just as you would change any property value. The new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method.

Source on msdn: Using Settings in C#

User settings are generally of use for persisting user preferences (e.g. app notification preferences etc.). Application settings would generally for items such as API keys etc.

As noted by @kmote, when user settings are modified and persisted at run time (via settings.Save()), they will be written to a folder within User Profile storage (typically C:\Users\Username\AppData\Local\AppName in Windows 7 and above). In order to determine the location of the file programmatically, please see this post.

like image 34
SpruceMoose Avatar answered Oct 13 '22 00:10

SpruceMoose