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来实现,先用这个顶着.
1 System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); 2 doc.Load("ConfigurationTest.exe.Config"); 3 4 XmlNodeList nodes = doc.ChildNodes[1].ChildNodes; 5 6 foreach (XmlNode node in nodes) 7 { 8 Console.WriteLine(node.InnerXml); 9 } 10 11 12 XmlNode newnode = doc.ChildNodes[1]; 13 14 foreach (XmlNode v in newnode.ChildNodes) 15 { 16 if (v.Name == "lastevent") 17 { 18 Console.WriteLine("lastevent 已经存在"); 19 return; 20 } 21 } 22 23 XmlElement elem = doc.CreateElement("lastevent"); 24 XmlAttribute att = doc.CreateAttribute("name"); 25 att.Value = "用于替换lastevent中不想看到的内容"; 26 elem.Attributes.Append(att); 27 28 29 XmlElement Items = doc.CreateElement("Items"); 30 elem.AppendChild(Items); 31 32 33 XmlElement add1 = doc.CreateElement("add"); 34 35 XmlAttribute original = doc.CreateAttribute("original"); 36 original.Value = "original"; 37 add1.Attributes.Append(original); 38 39 XmlAttribute replacement = doc.CreateAttribute("replacement"); 40 replacement.Value = "replacement"; 41 add1.Attributes.Append(replacement); 42 43 Items.AppendChild(add1); 44 45 elem.AppendChild(Items); 46 47 48 newnode.AppendChild(elem); 49 50 doc.Save("111.config");
二.用ConfigurationManager类来处理
上面提到的不用xml处理的方法,已经找到了.
原来配置文件为
运行代码后变成:
代码为:
1 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 2 3 LasteventSettingSection section = new LasteventSettingSection(); 4 section.Name = "替换"; 5 6 ConfigurationTest.Items its = new ConfigurationTest.Items(); 7 8 9 10 Item it = new Item(); 11 it.Original = "error"; 12 it.Replacement = "information"; 13 14 its.Add(it); 15 16 it = new Item(); 17 it.Original = "error2"; 18 it.Replacement = "information2"; 19 20 its.Add(it); 21 22 section.Items = its; 23 24 LasteventSettingSection lastevent = (LasteventSettingSection)config.Sections["lastevent"]; 25 if (lastevent == null) { 26 config.Sections.Add("lastevent", section); 27 } 28 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) >= 0){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!");}}
转载于:https://www.cnblogs.com/birds-zhu/p/6918119.html
app.config 配置多项 配置集合 自定义配置(4) 自动增加配置项到配置文件的两种方法...相关推荐
- 【短信发送】实现腾讯云发送短信功能--工具类和SpringBoot配置两种方法实现
实现腾讯云发送短信功能--工具类和SpringBoot配置两种方法实现 一.开通腾讯云短信服务 二.工具类--使用qcloudsms实现短信发送 三.Spring Boot项目引入短信发送功能 我们发 ...
- windows下tomcat集群配置(两种方法)
两种方法只是在配置上不同原理一样,因为apache2.X后其自身集成了mod_jk功能,相对于1.3版本,不需要再进行繁琐的worker.properties配置,配置过程大幅简化. 一.软件需求 操 ...
- 【错误记录】NDK 导入外部 so 动态库报错 ( java.lang.UnsatisfiedLinkError | Android Studio 配置外部 so 动态库两种方法 )
文章目录 一.报错信息 二.解决方案 ( Android Studio 配置外部 so 动态库两种方法 ) 1.jniLibs 目录存放 2.libs 目录存放 一.报错信息 外部引用 so 动态库 ...
- 配置 yum 源的两种方法
配置 yum 源的两种方法 由于 redhat的yum在线更新是收费的,如果没有注册的话不能使用,如果要使用,需将redhat的yum卸载后,重启安装,再配置其他源,以下为详细过程: 1.删除redh ...
- httos双向认证配置_APP爬虫双向认证抓包的两种方法
APP抓包相对繁琐,越来越多的 APP 在 https 请求和响应时,为了防止中间人攻击(或中间人抓包),会做证书认证,让抓包工具抓不到请求.证书认证分单向认证和双向认证,双向认证是相较于单向认证而言 ...
- 显示屏连接DL580服务器方法,DL380, DL580, DL360 服务器系列配置Raid 5的两种方法.doc...
ISSUE: 本文介绍DL380, DL580, DL360 服务器系列配置Raid 5的两种方法: 使用ORCA配置和使用Smart Start7.91中的Array Configuration U ...
- 详解mybatis的配置setMapperLocations多个路径两种方法
文章目录 一.配置文件方式 二.Javabean配置 前言:我们在平常工作中用到mybatis去加载Mapper.xml文件,可能mapper文件放的路径不一样,由此我们需要配置多个路径,幸运的是My ...
- 路由删除命令_清除思科路由器配置信息的两种方法
对于网络设备来说,清除思科路由器配置信息是防止黑客轻松地连接访问公司网络的最佳方法.在对Cisco路由器进行清理时,有两种不同的方法可供选择,而它们通常用于不同的方面: 一.知道路由器的密码,相关步骤 ...
- n76e003引脚图_N76E003 复位引脚(P20)配置为输入的两种方法
由于N76E003管脚比较少,17个标准通用管脚,另外还有一个只能做输入的引脚(RESET),在项目中就可能需要用到RESET引脚作为输入,那么问题来了,怎么配置RESET引脚当作输入呢?这里介绍两种 ...
最新文章
- k8s helm 私服chartmuseum minio s3 存储配置
- Android版本更新踩坑,Android Studio 3.0升级后踩到的坑
- 我两个月来对Quarkus的了解
- 递归调用方法时栈内存是如何变化的?(使用内存图演示递归调用过程)
- 4 网络、挂载、关机
- Brackets(POJ-2955)
- 青花瓷png免扣素材,中国风的标志素材
- php音频格式 操作,php操作视频音频类-ffmpeg-php
- 使用expect编写脚本
- 数据之路 - 数据可视化 - Tableau工具
- 【4-11】读书笔记 |《推荐系统实践》- 个性化推荐系统总结
- 迈信EP100伺服驱动器方案
- 老一辈学计算机的在那,真实的南京大学计算机系
- 【北交所周报】北交所再迎8只新股;康普化学、凯华材料上市首日逆势大涨;康乐卫士过会,或成北交所最大IPO;北交所推出直联机制...
- HTML学习笔记4:如何给网页添加图片和超链接
- Excel技巧:合并单元格后分组排序
- cygwin下编译报错 `addrinfo hints‘ has incomplete type and cannot be defined
- 软件测试之linux环境搭建与操作Xshell、Xftp
- python滤波与图像去噪
- 硅谷最牛程序员,总是不经意间碾压众人