一,按照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) 自动增加配置项到配置文件的两种方法的更多相关文章

  1. mybatis处理集合、数组参数使用in查询等语句的两种方法

    对于mybatis的参数类型是集合数组的时候进行查询. 第一种:参数list使用mybatis的标签 SELECT * FROM TABLE_NAME AS a <where> <i ...

  2. 配置 yum 源的两种方法

    配置 yum 源的两种方法 由于 redhat的yum在线更新是收费的,如果没有注册的话不能使用,如果要使用,需将redhat的yum卸载后,重启安装,再配置其他源,以下为详细过程:  1.删除red ...

  3. 最新阿里云服务器免费SSL证书配置HTTPS的两种方法(图文教程二)

    在大家学习如何利用免费SSL证书配置网站HTTPS之前,我们先要搞清楚为什么要开启HTTPS,这个绿色的小锁真的有用吗?所谓的HTTPS其实是(安全套接字层超文本传输协议)是以安全为目标的HTTP通道 ...

  4. CentOS 7配置静态IP地址的两种方法 来自:互联网

    CentOS 7配置静态IP地址的两种方法 来自:互联网 时间:2021-01-12 阅读:4 如果你想要为CentOS 7中的某个网络接口设置静态IP地址,有几种不同的方法,这取决于你是否想要使用网 ...

  5. Java代码中获取配置文件(config.properties)中内容的两种方法

    方法千千万,本人暂时只总结了两种方法. (1)config.properties中的内容如图 在applicationContext.xml中配置 <!-- 引入配置文件 --> < ...

  6. JAVA 集合 List 分组的两种方法

    CSDN日报20170219--<程序员的沟通之痛> [技术直播]揭开人工智能神秘的面纱 程序员1月书讯 云端应用征文大赛,秀绝招,赢无人机! JAVA 集合 List 分组的两种方法 2 ...

  7. 用Java集合中的Collections.sort方法对list排序的两种方法

    用Collections.sort方法对list排序有两种方法第一种是list中的对象实现Comparable接口,如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  8. 两种方法直接删除数组中特定值的项(JavaScript)

    一.问题详情: 直接删除意为原数组需要被改变,而不是得到另一个数组. 二.JavaScript实现 (一)巧用数组的push( ).shift( )方法 function del(arr,num) { ...

  9. app.config 配置多项 配置集合 自定义配置(2)

    上一篇说了利用app.config自定义节点配置,那是利用工具来实现,其实也一全部编码的方式来实现.举一个栗子.Simpson一家有父亲James,母亲Kate,和三个儿女Jim,Aaron和Luka ...

随机推荐

  1. Ubuntu16.04 install OpenJDK8

    1.按Ctrl + Alt + T打开终端.打开后,运行下面的命令来添加PPA:sudo add-apt-repository ppa:openjdk-r/ppa2.之后,更新系统包缓存并安装Open ...

  2. ZOJ1171

    错误代码先放这 #include<cstdio> #include<cstdlib> #include<iostream> #include<cstring& ...

  3. Jquery cookie操作示例,写入cookie,读取cookie,删除cookie

    <html> <head> <meta name="viewport" content="width=device-width" ...

  4. C#用SerialPort实现串口通讯

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. Spring MVC Ajax 复杂参数的批量传递

    要解决的问题: 如何组织客户端参数? Ajax 方法的配置属性如何定义才能传递复杂参数? 基于 SpringMVC 的服务端该如何接收? MyBatis 怎么处理批量更新? 客户端脚本 viewMes ...

  6. 知识树杂谈Java面试(4)

    一. Java集合 1.  集合分类:  Collection.Map. 2. Collection: 3. Map 4. 注意点 a. List 有序.可重复:Set 无序.不可重复:Map  键值 ...

  7. UEFI启动视频详解:启动分析+N项操作实例

    ============================================================= ※※※※最给力的视频解说※※※※ 2011hiboy全部共享资料:立刻去   ...

  8. [NOIP2014][DP]飞扬的小鸟

    [NOIP2014]飞扬的小鸟 ——!x^n+y^n=z^n 题目描述: Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画 ...

  9. vue 从入门到精通(二)

    上一篇总结了一些vue的理论知识,如果你没看懂的话--那返回去继续去看啊!反正我要开始第二篇了. vue提供了大量的指令,比如:v-if,v-bind,v-on--太多,多写项目,多看API,这里就不 ...

  10. Fastify 系列教程四 (求对象、响应对象和插件)

    Fastify 系列教程: Fastify 系列教程一 (路由和日志) Fastify 系列教程二 (中间件.钩子函数和装饰器) Fastify 系列教程三 (验证.序列化和生命周期) Fastify ...