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

Posted 瘦馬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了app.config 配置多项 配置集合 自定义配置 自动增加配置项到配置文件相关的知识,希望对你有一定的参考价值。

一,按照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!");
        }

    }

 

 

以上是关于app.config 配置多项 配置集合 自定义配置 自动增加配置项到配置文件的主要内容,如果未能解决你的问题,请参考以下文章

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

在Web.config或App.config中的添加自定义配置

在Web.config或App.config中的添加自定义配置

App.Config 自定义配置部分问题

在Web.config或App.config中的添加自定义配置

如何在 app.config 中创建自定义配置部分? [复制]