C# 为什么 app.config 中的应用程序设置是只读的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/758389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are application settings read-only in app.config?
提问by Blorgbeard is out
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.
我的 app.config 中有一些我打算成为“全局”的设置 - 即。任何用户都可以更改它们,并且所有用户都获得相同的设置。
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.
这实际上是一个 Windows 服务应用程序,它作为 LocalSystem 服务运行。它也可以由本地管理员使用参数“/config”手动运行,它会启动一个 Windows 窗体来编辑配置值。
So it will have write access to %PROGRAMFILES%
in both situations.
所以它%PROGRAMFILES%
在这两种情况下都有写访问权限。
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".
当 MySetting 设置为 Application(在我的项目属性中,Settings.settings)时,我收到一个编译时错误“MySetting 是只读的”。
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 使用教程,那将非常有帮助。
采纳答案by Jader Dias
The real complete answer:
真正完整的答案:
The app.config settings are read-only because there are 2 types of settings:
app.config 设置是只读的,因为有两种类型的设置:
- 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.
第一个不会改变,除非应用程序发布者发布它的新版本。第二个不存储在 app.config 中,而是存储在 user.config 文件中。在缺少这个 user.config 文件的情况下,app.config 提供了默认值。
If MySetting is a User Setting:
如果 MySetting 是用户设置:
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.
它将使用新设置创建一个user.config
文件[User Local Settings Application Data]\[company name]\[application].exe[hash string]\[version]
,这些设置将优先于app.config
文件中的设置。
回答by Henk Holterman
Why:Application settings are intended to be stored in the Application folder under Program Files where the user does not have write privileges.
原因:应用程序设置旨在存储在用户没有写入权限的 Program Files 下的 Application 文件夹中。
How:There is no default support for "All Users" but you should be able to setup your own custom config file in a public folder or use a Database.
如何:没有对“所有用户”的默认支持,但您应该能够在公共文件夹中设置自己的自定义配置文件或使用数据库。
回答by oscarkuo
Not quite sure what you mean here. Do you mean you allowed users to alter app.config from the UI and the changes are not persisted?
不太清楚你在这里的意思。您的意思是您允许用户从 UI 更改 app.config 并且不会保留更改吗?
did you call
你打电话了吗
ConfigurationManager.RefreshSection("appSettings");
and
和
Configuration.Save();
回答by Joel Coehoorn
One reason is that the app.config file is in your app's folder under the Program Files directory, and everything in Program Files is read only for standard users by default.
原因之一是 app.config 文件位于 Program Files 目录下的应用程序文件夹中,默认情况下,Program Files 中的所有内容对标准用户都是只读的。
Another is that app.config settings apply system wide. If one user makes a change it will impact other users. Normal users are not supposed to be able to make that kind of change. Anything that can impact multiple users should be set only a system administrator. Per-user settings belong in each user's Application Data folder.
另一个是 app.config 设置适用于系统范围。如果一个用户进行更改,它将影响其他用户。普通用户不应该能够进行这种更改。任何可能影响多个用户的东西都应该只设置为系统管理员。每用户设置属于每个用户的应用程序数据文件夹。
回答by Khaled Musaied
Configuration Settings are cached in the memory when you starts the application. you can deal with the app.config file as xml to change the values.
当您启动应用程序时,配置设置会缓存在内存中。您可以将 app.config 文件作为 xml 处理以更改值。
回答by Jeroen Landheer
Simply put: There's no location on a machine that everyone can change, unless you give privileges to do so.
简单地说:机器上没有每个人都可以更改的位置,除非您授予这样做的权限。
There are several ways to deal with this kind of situation:
有几种方法可以处理这种情况:
You can create a configuration file / some registry settings, put this in the "all users" profile and grant "Everyone" the rights to change that specific file. During installation you can automate the procedure for granting the appropiate privileges and your program can handle the rest.
You can leverage UAC to make sure the current user has the appropiate privileges to change a system-wide setting. This is the recommended approach but also means that not everyone can change specific settings.
You can use a shared database and store your settings in there.
???
您可以创建一个配置文件/一些注册表设置,将其放在“所有用户”配置文件中并授予“所有人”更改该特定文件的权限。在安装过程中,您可以自动执行授予适当权限的过程,而您的程序可以处理其余的工作。
您可以利用 UAC 来确保当前用户具有更改系统范围设置的适当权限。这是推荐的方法,但也意味着不是每个人都可以更改特定设置。
您可以使用共享数据库并将您的设置存储在那里。
???
I would not recommend to change items in the program files directory or changing the default privileges overthere.
我不建议更改程序文件目录中的项目或更改那里的默认权限。
EDIT: As local system you have indeed write privileges to the program files directory. If you get the "Read only" error, it means the settings itself are read only. You'll need to use the configuration manager to be able to change the settings in configuration files.
编辑:作为本地系统,您确实具有对程序文件目录的写入权限。如果您收到“只读”错误,则表示设置本身是只读的。您需要使用配置管理器才能更改配置文件中的设置。
Hope this helps.
希望这可以帮助。