Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...