如何更改运行时应用程序设置
我试图在运行时更改应用程序设置文件的一键,但它不起作用。
我就是这样做的:
ConfigurationSettings.AppSettings["XPTO"] = "HELLO";
似乎它只在内存中发生变化,而不是在文件上发生变化。
有谁知道该怎么做?
谢谢。
I'm trying to change in runtime one key of my applications settings file, but it does not work.
I do on that way:
ConfigurationSettings.AppSettings["XPTO"] = "HELLO";
It seems that it only changes in memory, not on the file.
Does anyone knows how to do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看我的 .NET 设置概述文件...简而言之,我认为您需要一个用户范围的设置。 它的行为会更像你期望的那样。
Take a look at my overview of .NET settings files...In short, I think you want a user-scoped setting. It will behave more like you expect.
假设您的应用程序对文件具有写权限...
我忽略了所有可能引发的异常...
Assuming your app has write permissions on the file...
I'm ignoring all the possible exceptions that can be thrown...
AppSettings 文件未设计为可写。 它旨在存储在运行时不会更改但可能会随时间更改的配置,即:数据库连接字符串、Web 服务 URL 等。
因此,虽然实际上可能会更新文件,但您应该重新评估是否这样做值应该存储在那里。
The AppSettings file is not designed to be writable. It is designed to store configurations that will not change at run time but might change over time ie: DB Connection Strings, web service URL's, etc.
So, while it may be possible to update the file in reality you should re-asses if this value should be stored there.