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 ...
随机推荐
- h5 meta学习
定义针对搜索引擎的关键词:<meta name="keywords" content="meta,red" /> 定义对页面的描述:<meta ...
- 富文本编辑器CKEditor的使用
由于最近在架构一个pc端b/s结构的项目,项目中有个论坛模块,当用户发帖时,需要用到富文本编辑器,考虑了一下,决定使用CKEditor富文本编辑器,虽然现在问世的富文本编辑器很丰富,比如还有百度的UE ...
- Python实战之int学习笔记及简单练习
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__ ...
- ES6的变量解构赋值
前 言 ES6 解构赋值: ES6允许按照一定模式从数组和对象中提取值,然后对变量进行赋值,这被称为解构. 1.1 数组的结构赋值 1.1.1基本用法 JS中,为变量赋值直接指定.例如下面代码: ...
- 作为前端Web开发者,这12个终端命令不可不会
对于开发人员来说,终端是最重要的工具之一.掌握终端,能够有效的提升开发人员的工作流程.使用终端,许多日常任务都被简化为了编写简单的命令并按下 Enter 按钮. 本文列举了一系列 Linux 命令,旨 ...
- nodejs+mongoose操作mongodb副本集实例
继上一篇设置mongodb副本集之后,开始使用nodejs访问mongodb副本集: 1:创建项目 express 项目名称 2:npm install mongoose 安装mongo ...
- Emgu.CV(一)
由于这块的知识不少,会分好几期写完 什么是OpenCV? OpenCV是一个基于(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows和Mac OS操作系统上.它轻量级而且高效--由一 ...
- struts2使用模型传值
用户bean package userBeans; public class User { private String username; public String getUsername() { ...
- 从canvas理解面向对象
前言 据说在编程语言的发展过程中,面向对象语言的出现是为了解决GUI编程的问题而出现的.计算机一开始是用纸带,命令行等来和人进行交互,而图形界面的出现是一次重大的改进,使普通人很容易就能使用计算机. ...
- JS的基本类型(小知识)
一:js中的基本类型: 基本类型:boolen, string ,number,null,undefined 引用类型:object,和函数 二.undedifned和null的区别: 1 undef ...