为什么 app.config 中的应用程序设置是只读的? [英] Why are application settings read-only in app.config?
问题描述
我的 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.
这是为什么?
我应该如何保留应用的全局设置?
And how should I go about persisting my app's global settings?
这实际上是一个 Windows 服务应用程序,它作为 LocalSystem 服务运行.它也可以由本地管理员使用参数/config"手动运行,它会启动一个 Windows 窗体来编辑配置值.
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.
所以它在两种情况下都可以写%PROGRAMFILES%
.
So it will have write access to %PROGRAMFILES%
in both situations.
我访问我的设置的方式是这样的:
The way I am accessing my settings is thusly:
Settings.Default.MySetting = MyNewValue;
当 MySetting 设置为 Application(在我的项目属性中,Settings.settings)时,我收到一个编译时错误MySetting is read only".
And when MySetting is set to Application (in my project properties, Settings.settings), I get a compile-time error "MySetting is read only".
我是这个东西的新手,还没有找到一个很好的解释它应该如何完成.例如,为什么我需要说默认",这实际上是什么意思?我不知道.如果有人可以给我指点 app.config 使用教程,那将非常有帮助.
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.
推荐答案
真正完整的答案:
app.config 设置是只读的,因为有两种类型的设置:
The app.config settings are read-only because there are 2 types of settings:
- 应用设置
- 用户设置
第一个不会改变,除非应用程序发布者发布它的新版本.第二个不存储在 app.config 中,而是存储在 user.config 文件中.在缺少这个 user.config 文件的情况下,app.config 提供了默认值.
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.
如果 MySetting 是用户设置:
If MySetting is a User Setting:
Settings.Default.MySetting = MyNewValue;
Settings.Default.Save();
它将在 [User Local Settings Application Data]\[company name]\[application].exe[hash string]\[version]
创建一个 user.config
文件code> 使用新设置,这些设置将优先于 app.config
文件中的设置.
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.
这篇关于为什么 app.config 中的应用程序设置是只读的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!