1

If I create a user level application setting and bind it to a text box on a form, then type something in the textbox, the value is automatically saved and when the application is launched again the value appears in the textbox. This doesn't happen when I scope the setting as application.

Why are application scoped settings not saved automatically like user scoped settings are?

If this is by design, how can I manually save these settings and load them at runtime?

Jimmy D
  • 5,282
  • 16
  • 54
  • 70

1 Answers1

4

In short, application scoped settings can't be changed at runtime while user scoped settings are designed be read/written at runtime. There are a number of alternatives to using application scoped settings:

  • Use user scoped settings
  • This post recommends looking into the ConfigurationManager class.
  • If you don't like user scoped settings because they are not written to the application directory you can implementing your own SettingsProvider and have them written where ever you'd like

This post has some recommendations on how to best manage settings. Finally, here is Microsoft's documentation on the settings architecture if you're interested in extending theirs or rolling your own.

Community
  • 1
  • 1
Dan Busha
  • 3,723
  • 28
  • 36
  • So application-scoped settings are to be set at design time via the properties window, and then left alone? They aren't written to disk and then loaded from a file at run time? – Jimmy D Apr 13 '12 at 18:27
  • @YourMother sort of. Default values for application-scoped settings will be compiled into your application (see Properties folder -> Settings.settings where the designer writes your settings). These values can also be override in the MyApp.exe.config file after the the application is compiled. – Dan Busha Apr 13 '12 at 18:41
  • Thanks. I tested this and that seems to be how it works. I can indeed override the default setting be editing the .exe.config file. My question is, if I delete this file then the app seems to load the default again. So it does appear to be compiled into the app. What would happen if the config file was accidentally deleted and it had been edited with new values? Can it be regenerated somehow with default values again? Just trying to understand the behavior. – Jimmy D Apr 13 '12 at 19:07
  • @YourMother I believe what happens is that the application checks to see if an .config file exists and loads it (if it exists), else it loads the default values. User scoped settings are a little bit safer because they are written to C:\Users\\AppData\Local\ so it's harder to lose/overwrite them. The config manager knows where to load them from, but this can cause further complications (upgrades, not very portable, etc) – Dan Busha Apr 13 '12 at 19:37