将自定义部分写入 app.config
我想将一些自定义数据保存到应用程序配置文件中,我需要在 app.config 中创建一些自定义部分.从 app.config 读取自定义数据是一项简单的任务,但我无法将程序中的信息写入 app.config.为了找到这个问题的解决方案,我创建了测试项目.
I want to save some custom data into application configuration file and I need to create some custom sections in app.config. Reading custom data from app.config is simple task, but I can't write information from my programm into app.config. For finding solution of this problem I create test project.
为了从自定义部分 app.config 读取数据,我使用了本文中的信息:
For reading data from custom section app.config I used information from this article:
首先覆盖 CustomConfigSection
中的 IsReadyOnly()
以返回 false.
First override IsReadyOnly()
in your CustomConfigSection
to return false.
一旦你这样做了,你应该能够做这样的事情(这是针对 ASP.NET,但它可能是可转移的):
Once you've done that you should be able to do something like this (this is for ASP.NET, but it might be transferably):
System.Configuration.Configuration configFile = WebConfigurationManager.OpenWebConfiguration("~");
CustomConfigSection config = (CustomConfigSection)configFile.GetSection("Custom");
config.Tweak = 1;
config.Change = "foo";
configFile.Save();
试试看.