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 ...
随机推荐
- Python s12 Day3 笔记及作业
1. Set集合 old_dict = { "#1":{ 'hostname':'c1', 'cpu_count':2, 'mem_capicity':16}, "#2& ...
- 从头编写 asp.net core 2.0 web api 基础框架 (1)
工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相 ...
- python修改注册表
与注册表操作相关的函数可以分为打开注册表.关闭注册表.读取项值.c添加项值.添加项,以及删除项等几类. 表1 Windows注册表基本项 项名 描述 HKEY_CLASSES_ROOT 是HKEY ...
- 虚拟机中ubuntu-16.04 Linux系统下配置mysql数据库,并在windows下使用navicat远程连接
Linux系统下mysql数据库安装配置步骤: 1.在服务器上安装mysql:sudo apt-get install mysql-server sudo apt-get install mysql- ...
- 正则表达式与grep和sed
正则表达式与grep和sed 目录 1.正则表达式 2.grep 3.sed grep和sed需要正则表达式,我们需要注意的正则表达式与通配符用法的区分. 1.正则表达式 REGEXP,正则表达式:由 ...
- 2017-03-02学习心得之Java代码
package com.lovo.classes;import java.util.Random;import java.util.TreeSet;import java.util.Scanner;p ...
- python 携带cookie访问网站(python接口测试post)
最近在使用自己研究性能测试工具的时候想到,使用python向服务器不断发送数据以作为并发测试.大概情况如下: #coding=utf-8 import urllib2 import urllib im ...
- linux下如何安装破解IntelliJ IDEA,及其基本使用教程;
今天在linux下安装了IntelliJ idea,由于现在很多企业在linux平台下使用IntelliJ idea做java web的开发,所以对于IntelliJ idea的安装和学习是一件基本的 ...
- UVa816,Ordering Tasks,WA
#include <iostream> #include <cstdio> #include <string> #include <cstring> # ...
- [原创]InnoDB体系结构
参阅:<innodb存储引擎内幕> innodb整体的体系结构如下图所示: 整体结构分两大部分:内存和进程其中内存包括:buffer_pool\redo log buffer\addit ...