为什么应用程序设置只读的app.config? [英] Why are application settings read-only in app.config?
问题描述
我在我的app.config中的一些设置,我打算成为全球性 - 即。任何用户都可以改变他们,并且所有用户获得相同的设置。
不过,除非我改变他们为用户设置,它们是只读的。
这是为什么?
和我应该如何去坚持我的应用程序的全局设置?
编辑:
这实际上是为运行作为LocalSystem服务Windows服务应用程序。它也可以由本地管理员以参数/配置,这将启动一个Windows表单编辑配置值手动运行。
因此,将有写权限< 。code>%PROGRAMFILES%在这两种情况下
我访问我的设置方式是正是如此:
Settings.Default.MySetting = MyNewValue;
当MySetting设置为应用程序(在我的项目属性,Settings.settings),我得到一个编译时错误MySetting是只读的。
我是新来这个东西,还没有找到它应该是怎样做一个很好的解释。例如,为什么我需要说默认,又是什么,实际上意味着什么呢?我不知道。如果任何人都可以点我一个app.config使用教程,这将是真正的帮助。
真正的完整的答案:
在app.config设置只读的,因为有2个类型的设置:
<醇>
第一个也不会,除非应用程序更改发布者发布的一个新的版本。第二不被存储在在app.config,但在一个user.config文件。在这种user.config文件的app.config提供了默认值的abscence
如果MySetting为用户设置:
Settings.Default.MySetting = MyNewValue;
Settings.Default.Save();
这将创建一个 user.config
文件在 [用户本地设置应用程序数据] \ [公司名称] \ [应用] .exe文件[哈希字符串] \ [版本]
通过新的设置,以及那些设置将战胜在的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.
The real complete answer:
The app.config settings are read-only because there are 2 types of settings:
- Application Settings
- 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.
这篇关于为什么应用程序设置只读的app.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!