app.config 配置多项 配置集合 自定义配置(4) 自动增加配置项到配置文件的两种方法
一,按照xml文件处理:
配置文件如下图(最后的图片).
自动写入configSections和configSections的实例
1.自动写入configSections
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
LasteventSettingSection last = new LasteventSettingSection();
config.Sections.Add("lastevent", last);
config.Save();
2.自动写入实例
我觉得不应该将.config文件当成xml来操作.但是一直没有找到方法用ConfigurationManager来实现,先用这个顶着.
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load("ConfigurationTest.exe.Config"); XmlNodeList nodes = doc.ChildNodes[].ChildNodes; foreach (XmlNode node in nodes)
{
Console.WriteLine(node.InnerXml);
} XmlNode newnode = doc.ChildNodes[]; foreach (XmlNode v in newnode.ChildNodes)
{
if (v.Name == "lastevent")
{
Console.WriteLine("lastevent 已经存在");
return;
}
} XmlElement elem = doc.CreateElement("lastevent");
XmlAttribute att = doc.CreateAttribute("name");
att.Value = "用于替换lastevent中不想看到的内容";
elem.Attributes.Append(att); XmlElement Items = doc.CreateElement("Items");
elem.AppendChild(Items); XmlElement add1 = doc.CreateElement("add"); XmlAttribute original = doc.CreateAttribute("original");
original.Value = "original";
add1.Attributes.Append(original); XmlAttribute replacement = doc.CreateAttribute("replacement");
replacement.Value = "replacement";
add1.Attributes.Append(replacement); Items.AppendChild(add1); elem.AppendChild(Items); newnode.AppendChild(elem); doc.Save("111.config");

二.用ConfigurationManager类来处理
上面提到的不用xml处理的方法,已经找到了.
原来配置文件为

运行代码后变成:

代码为:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
LasteventSettingSection section = new LasteventSettingSection();
section.Name = "替换";
ConfigurationTest.Items its = new ConfigurationTest.Items();
Item it = new Item();
it.Original = "error";
it.Replacement = "information";
its.Add(it);
it = new Item();
it.Original = "error2";
it.Replacement = "information2";
its.Add(it);
section.Items = its;
LasteventSettingSection lastevent = (LasteventSettingSection)config.Sections["lastevent"];
if (lastevent == null) {
config.Sections.Add("lastevent", section);
}
config.Save();
实现的类如下:
注意:前面几个例子中,继承ConfigurationElementCollection的Items没有实现add,remove方法,必须实现才可以.
class LasteventSettingSection : System.Configuration.ConfigurationSection
{
[ConfigurationProperty("name", IsRequired = false)]
public string Name
{
get { return (string)base["name"]; }
set { base["name"] = value; }
} [ConfigurationProperty("items", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(Item), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap, RemoveItemName = "remove")]
public Items Items
{
get { return (Items)base["items"]; }
set { base["items"] = value; }
}
} class Item : ConfigurationElement
{
[ConfigurationProperty("original", IsRequired = true, IsKey = true)]
public string Original
{
get { return (string)base["original"]; }
set { base["original"] = value; }
}
[ConfigurationProperty("replacement", IsRequired = true)]
public string Replacement
{
get { return (string)base["replacement"]; }
set { base["replacement"] = value; }
}
} class Items : ConfigurationElementCollection
{
protected override object GetElementKey(ConfigurationElement element)
{
return ((Item)element).Original;
} protected override ConfigurationElement CreateNewElement()
{
return new Item();
} public Item this[int i]
{
get { return (Item)base.BaseGet(i); }
} new public Item this[string key]
{
get { return (Item)base.BaseGet(key); }
} public int IndexOf(Item url)
{
return BaseIndexOf(url);
} public void Add(Item url)
{
BaseAdd(url); // Your custom code goes here. } protected override void BaseAdd(ConfigurationElement element)
{
BaseAdd(element, false); // Your custom code goes here. } public void Remove(Item url)
{
if (BaseIndexOf(url) >= )
{
BaseRemove(url.Original);
// Your custom code goes here.
Console.WriteLine("UrlsCollection: {0}", "Removed collection element!");
}
} public void RemoveAt(int index)
{
BaseRemoveAt(index); // Your custom code goes here. } public void Remove(string name)
{
BaseRemove(name); // Your custom code goes here. } public void Clear()
{
BaseClear(); // Your custom code goes here.
Console.WriteLine("UrlsCollection: {0}", "Removed entire collection!");
} }
app.config 配置多项 配置集合 自定义配置(4) 自动增加配置项到配置文件的两种方法的更多相关文章
- mybatis处理集合、数组参数使用in查询等语句的两种方法
对于mybatis的参数类型是集合数组的时候进行查询. 第一种:参数list使用mybatis的标签 SELECT * FROM TABLE_NAME AS a <where> <i ...
- 配置 yum 源的两种方法
配置 yum 源的两种方法 由于 redhat的yum在线更新是收费的,如果没有注册的话不能使用,如果要使用,需将redhat的yum卸载后,重启安装,再配置其他源,以下为详细过程: 1.删除red ...
- 最新阿里云服务器免费SSL证书配置HTTPS的两种方法(图文教程二)
在大家学习如何利用免费SSL证书配置网站HTTPS之前,我们先要搞清楚为什么要开启HTTPS,这个绿色的小锁真的有用吗?所谓的HTTPS其实是(安全套接字层超文本传输协议)是以安全为目标的HTTP通道 ...
- CentOS 7配置静态IP地址的两种方法 来自:互联网
CentOS 7配置静态IP地址的两种方法 来自:互联网 时间:2021-01-12 阅读:4 如果你想要为CentOS 7中的某个网络接口设置静态IP地址,有几种不同的方法,这取决于你是否想要使用网 ...
- Java代码中获取配置文件(config.properties)中内容的两种方法
方法千千万,本人暂时只总结了两种方法. (1)config.properties中的内容如图 在applicationContext.xml中配置 <!-- 引入配置文件 --> < ...
- JAVA 集合 List 分组的两种方法
CSDN日报20170219--<程序员的沟通之痛> [技术直播]揭开人工智能神秘的面纱 程序员1月书讯 云端应用征文大赛,秀绝招,赢无人机! JAVA 集合 List 分组的两种方法 2 ...
- 用Java集合中的Collections.sort方法对list排序的两种方法
用Collections.sort方法对list排序有两种方法第一种是list中的对象实现Comparable接口,如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 两种方法直接删除数组中特定值的项(JavaScript)
一.问题详情: 直接删除意为原数组需要被改变,而不是得到另一个数组. 二.JavaScript实现 (一)巧用数组的push( ).shift( )方法 function del(arr,num) { ...
- app.config 配置多项 配置集合 自定义配置(2)
上一篇说了利用app.config自定义节点配置,那是利用工具来实现,其实也一全部编码的方式来实现.举一个栗子.Simpson一家有父亲James,母亲Kate,和三个儿女Jim,Aaron和Luka ...
随机推荐
- ASP.NET没有魔法——目录
ASP.NET没有魔法——开篇-用VS创建一个ASP.NET Web程序 ASP.NET没有魔法——为什么使用ASP.NET ASP.NET没有魔法——第一个ASP.NET应用<MyBlog&g ...
- 使用HDFS客户端java api读取hadoop集群上的信息
本文介绍使用hdfs java api的配置方法. 1.先解决依赖,pom <dependency> <groupId>org.apache.hadoop</groupI ...
- 创建mongodb副本集操作实例
一:概念 相关概念及图片引用自这里 mongodb副本集: 副本集是一组服务器,其中一个是主服务器,用于处理客户请求:还有多个备份服务器,用于保存主服务器的数据副本.如果主服务器崩溃了,备份服务器自动 ...
- Ubuntu 16.04 LTS 下安装MATLAB2015b 以及Matlab system error解决办法
下载MATLAB2015b破解版 操作系统:Ubuntu 16.o4 LTS 程序文件:Matlab2015b-glnxa64破解版 解压提取文件:在ubuntu系统下可以直接提取压缩文件,得到三个文 ...
- 从零开始搭建框架SSM+Redis+Mysql(一)之摘要
从零开始搭建框架SSM+Redis+Mysql(一)之摘要 本文章为本人实际的操作后的回忆笔记,如果有步骤错漏,希望来信307793969@qq.com或者评论指出. 本文章只体现过程,仅体现操作流程 ...
- OMP算法代码学习
正交匹配追踪(OMP)算法的MATLAB函数代码并给出单次测试例程代码 测量数M与重构成功概率关系曲线绘制例程代码 信号稀疏度K与重构成功概率关系曲线绘制例程代码 参考来源:http://blog ...
- Fastify 系列教程一(路由和日志)
介绍 Fastify是一个高度专注于以最少开销和强大的插件架构,为开发人员提供最佳体验的Web框架. 它受到了 Hapi 和 Express 的启发,是目前最快的 Node 框架之一. Fastify ...
- javascript入门知识点总结(一)
学了几天javascript,现总结一下原生javascript的基本知识点. 一.javascript基本知识 变量 定义: var a = 123; var b = 'abc'; //连写 var ...
- base64减少图片请求
1. 使用base64减少 a) 2. 页面解析 CSS 生成的 CSSOM 时间增加 Base64 跟 CSS 混在一起,大大增加了浏览器需要解析CSS树的耗时.其实解析CSS ...
- LeetCode 495. Teemo Attacking (提莫攻击)
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...