app.config 配置多项 配置集合 自定义配置(4) 自动增加配置项到配置文件的两种方法...
生活随笔
收集整理的這篇文章主要介紹了
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) 自动增加配置项到配置文件的两种方法...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP+mysql+ajax搭建图书管理
- 下一篇: u启动 装系统