101

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?

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Parag Meshram
  • 8,281
  • 10
  • 52
  • 88

2 Answers2

85

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
mcalex
  • 6,628
  • 5
  • 50
  • 80
  • can u plz explain using an example? – Parag Meshram Nov 12 '13 at 13:36
  • 1
    Default settings are in the config file, yes? But where are the saved user settings stored? – Kyle Delaney Jun 28 '17 at 20:03
  • 10
    App settings are saved to the *.exe.config file. User settings get saved to c:\users\\AppData\Local\\\\user.config (where the later are pulled from assembly file). On load, user settings take precedence over app settings. – Schrodo_Baggins Jul 27 '17 at 14:12
  • @Schrodo_Baggins seems the user settings inside the `.exe.config` file get used as defaults for the `user.config` though. – Nyerguds Aug 12 '22 at 07:22
74

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.

SpruceMoose
  • 9,737
  • 4
  • 39
  • 53
  • any live example would be much appreciated – Parag Meshram Nov 12 '13 at 13:37
  • 1
    It should also be pointed out that User settings cannot be changed by editing the .config file between sessions. – kmote Aug 31 '16 at 19:00
  • @kmote Of course you can! If the session is closed, you can change the config (and therefore the userSetting). The change persists and reflects in the program when you start the session again... I tested it just now. – Riegardt Steyn Mar 29 '17 at 09:06
  • 5
    @Heliac: I should have been more precise. You are correct that userSettings can be changed between sessions. HOWEVER, once you change them programmatically (using `Settings.Save()`), a _new_ config file is created and is hidden/buried in C:\Users\[username]\AppData\Local\Microsoft. From that point on, whenever the application is started the user settings will be drawn from _that_ file exclusively, and any changes make manually in App.config or Settings.settings will be ignored. – kmote Mar 29 '17 at 16:20
  • Is there a way to find and view the contents of this user config file? – Kyle Delaney Jun 28 '17 at 20:08
  • See https://stackoverflow.com/questions/1075204/when-using-a-settings-settings-file-in-net-where-is-the-config-actually-stored – SpruceMoose Jun 28 '17 at 21:19