首页 > 移动平台 > 详细

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

时间:2017-05-29 15:43:18      阅读:362      评论:0      收藏:0      [点我收藏+]

配置文件如下图(最后的图片).

自动写入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");

 

技术分享

 

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

原文:http://www.cnblogs.com/birds-zhu/p/6918119.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!