Domanda

My program has some settings built in which all have a User Scope. However when the program launches it only creates the AppName.exe.config file, which contains the settings .

When saving a setting later on during runtime, it creates the user.config file (which previously didn't exist) in the AppData/Local/AppName/ location, but that file only contains the saved setting.

Why does this happen? Why won't it create the user.config or use that on startup if it exists?

È stato utile?

Soluzione

From Application Settings Architecture at MSDN:

  1. Application-scoped settings can be stored in either the machine.config or app.exe.config files. Machine.config is always read-only, while app.exe.config is restricted by security considerations to read-only for most applications.

  2. User-scoped settings can be stored in app.exe.config files, in which case they are treated as static defaults.

  3. Non-default user-scoped settings are stored in a new file, user.config, where user is the user name of the person currently executing the application. You can specify a default for a user-scoped setting with DefaultSettingValueAttribute. Because user-scoped settings often change during application execution, user.config is always read/write.

What you're seeing first is (what you've called) your "built in settings" being stored as (what Microsoft calls) "static default" user-scoped settings, which get stored in app.exe (as per 2).

And then when you write back your settings at runtime, they're treated as "non-default" user-scoped settings, and they're being written to user.config (as per 3) hence why only then do you see the user.config file created.

In short, there's no need for a per-user user.config file, as long as the user-scoped settings are the same (defaults) for everyone.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top