我定义了操作配置文件的三个类,代码如下:
//这个是元素类
public class PluginConfigurationElement : ConfigurationElement
{
        public PluginConfigurationElement()
        {
        }        [ConfigurationProperty("Name")]
        public String Name
        {
            get
            {
                return (String)this["Name"];
            }
            set
            {
                this["Name"] = value;
            }
        }
}//这个是配置节点类
public class PluginConfigurationSection : ConfigurationSection
{
        public PluginConfigurationSection()
        {
        }        [ConfigurationProperty("", IsDefaultCollection = true)]
        public PluginConfigurationCollection PluginCollection
        {
            get
            {
                return (PluginConfigurationCollection)base[""];
            }
            set
            {
                base[""] = value;
            }
        }
}//这个是配置元素集合类
public class PluginConfigurationCollection : ConfigurationElementCollection
{
        public PluginConfigurationCollection()
        {
            //PluginConfigurationElement e = (PluginConfigurationElement)CreateNewElement();
            //Add(e);
        }        protected override void
           BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
        }        public void Remove(PluginConfigurationElement pluginElement)
        {
            if (BaseIndexOf(pluginElement) >= 0)
                BaseRemove(pluginElement.Name);
        }        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }
        ....省略部分代码
}然后我在程序中调用这些方法往配置文件中写入信息,代码如下:Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
PluginConfigurationSection ps = (PluginConfigurationSection)config.Sections["PluginSection"];
PluginConfigurationElement pe = new PluginConfigurationElement();
pe.Name = pluginName;
ps.PluginCollection.Add(pe);
config.Sections.Add("add", ps);
config.Save(ConfigurationSaveMode.Minimal);代码在执行config.Sections.Add("add", ps);这里的时候提示异常“无法添加已属于该配置的 ConfigurationSection。”
现有的app.config文件的内容是这样的:<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section
      name="PluginSection"
      type="PluginFramework.PluginConfigurationSection, PluginFramework"
      />    
  </configSections> 
  <PluginSection>
    
  </PluginSection>
</configuration>我需要的是能在PluginSection节点里添加多个add元素,并且每个add元素中都有属性name,即效果是这样的:<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section
      name="PluginSection"
      type="PluginFramework.PluginConfigurationSection, PluginFramework"
      />    
  </configSections> 
  <PluginSection>
    <add name="plugin1" />
    <add name="plugin2" />
  </PluginSection>
</configuration>请问我代码需要如何修改才能实现这样的功能?

解决方案 »

  1.   

    new 一个新的自定义配置对象
      

  2.   


    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="PluginSection" type="PluginFramework.PluginConfigurationSection, PluginFramework"/>    
      </configSections> 
      <PluginSection>
        <add name="plugin1" />
        <add name="plugin2" />
      </PluginSection>
    </configuration>我是要configSections节点里的section节点就这一个,而section里的name属性的值所对应的节点PluginSection,它里面的节点可以有多个,如何new一个新的配置对象,new PluginConfigurationSection()?