C# – Why are application settings read-only in app.config

app-configcnet

I have some settings in my app.config which I intend to be 'global' – ie. any user can change them, and all users get the same setting.

But unless I change them to be user settings, they are read only.

Why is this?

And how should I go about persisting my app's global settings?

Edit:

This is actually a windows service application which runs as a service as LocalSystem. It can also be run manually by a local admin with argument "/config", which launches a windows form to edit configuration values.

So it will have write access to %PROGRAMFILES% in both situations.

The way I am accessing my settings is thusly:

Settings.Default.MySetting = MyNewValue;

And when MySetting is set to Application (in my project properties, Settings.settings), I get a compile-time error "MySetting is read only".

I am new to this stuff, and have not yet found a very good explanation of how it is supposed to be done. For example, why do I need to say 'Default', and what does that actually mean? I have no idea. If anyone can point me to an app.config usage tutorial, that would be really helpful.

Best Answer

The real complete answer:

The app.config settings are read-only because there are 2 types of settings:

  1. Application Settings
  2. User Settings

The first won't change unless the application publisher publishes a new version of it. The second is not stored in the app.config, but in a user.config file. In the abscence of this user.config file the app.config provides the default value.

If MySetting is a User Setting:

Settings.Default.MySetting = MyNewValue;
Settings.Default.Save();

It will create a user.config file at [User Local Settings Application Data]\[company name]\[application].exe[hash string]\[version] with the new settings, and those settings will prevail over the settings in the app.config file.