将自定义部分写入 app.config [英] Writing custom sections into app.config

查看:36
本文介绍了将自定义部分写入 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:

http://devlicio.us/blogs/derik_whittaker/archive/2006/11/13/app-config-and-custom-configuration-sections.aspx

推荐答案

首先覆盖 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();

试试看.

这篇关于将自定义部分写入 app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆