Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are application settings read-only in app.config?

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.

like image 201
Blorgbeard Avatar asked Apr 16 '09 23:04

Blorgbeard


People also ask

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.

Where are C# application settings stored?

There is a folder called "Properties" under your project root folder, and there are *. settings file under that folder. That's where it gets stored.


1 Answers

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.

like image 137
Jader Dias Avatar answered Sep 29 '22 21:09

Jader Dias