日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

app.config 配置多项 配置集合 自定义配置(4) 自动增加配置项到配置文件的两种方法...

發布時間:2025/7/25 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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) 自动增加配置项到配置文件的两种方法...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。