VS App.config文件读写(自定义增加节点)

VS App.config文件读写(自定义增加节点)
若只是不想增加xml文件而利用app.config,可使用appSettings

项目中增加文件

节点元素、集合和一个汇总cs。先在项目中创建文件夹,再将各个cs文件添加进去
大致总览

节点集合

using System.Configuration;

namespace WindowsFormsApplication3.UserConfigutarion
{
    class BestPointPositionCollection:ConfigurationElementCollection
    {
        public BestPointPositionElement this[int index]
        {
            get
            {
                return (BestPointPositionElement)BaseGet(index);
            }

            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);

                BaseAdd(index, value);
            }
        }

        public new BestPointPositionElement this[string key]
        {
            get
            {
                return (BestPointPositionElement)BaseGet(key);
            }

            set
            {
                if (BaseGet(key) != null)
                    BaseRemoveAt(BaseIndexOf(BaseGet(key)));
                BaseAdd(value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new BestPointPositionElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((BestPointPositionElement)element).Value;
        }
    }
}

节点元素

using System.Configuration;

namespace WindowsFormsApplication3.UserConfigutarion
{
    class BestPointPositionElement:ConfigurationElement
    {
        [ConfigurationProperty("x", IsKey = true, IsRequired = true,DefaultValue="0")]
        public string X
        {
            get
            {
                return (string)base["x"];
            }
            set
            {
                base["x"] = value;
            }
        }

        [ConfigurationProperty("y", IsKey = true, IsRequired = true,DefaultValue = "0")]
        public string Y
        {
            get
            {
                return (string)base["y"];
            }
            set
            {
                base["y"] = value;
            }
        }

        [ConfigurationProperty("value", IsKey = true, IsRequired = true, DefaultValue = "0")]
        public string Value
        {
            get
            {
                return (string)base["value"];
            }
            set
            {
                base["value"] = value;
            }
        }
    }

}

汇总cs

using System.Configuration;

namespace WindowsFormsApplication3.UserConfigutarion
{
    class userConfig:ConfigurationSection
    {
        [ConfigurationProperty("start")]
        [ConfigurationCollection(typeof(BestPointPositionCollection))]
        public BestPointPositionCollection startsection
        {
            get
            {
                return (BestPointPositionCollection)this["start"];
            }
        }

        [ConfigurationProperty("part")]
        [ConfigurationCollection(typeof(BestPointPositionCollection))]
        public BestPointPositionCollection partsection
        {
            get
            {
                return (BestPointPositionCollection)this["part"];
            }
        }

        [ConfigurationProperty("axis")]
        [ConfigurationCollection(typeof(StartLoadCollection))]
        public StartLoadCollection axissection
        {
            get
            {
                return (StartLoadCollection)this["axis"];
            }
        }

        [ConfigurationProperty("pulseEquivalent")]
        [ConfigurationCollection(typeof(StartLoadCollection))]
        public StartLoadCollection pulseequivalentsection
        {
            get
            {
                return (StartLoadCollection)this["pulseEquivalent"];
            }
        }
    }
}

app.config

要在第一个节点位置增加说明信息

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="usersettings" type="WindowsFormsApplication3.UserConfigutarion.userConfig,WindowsFormsApplication3"/>
    <section name="bestposition" type="WindowsFormsApplication3.UserConfigutarion.userConfig,WindowsFormsApplication3"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <usersettings>
    <axis>
      <add x="3" y="9" z="2"/>
    </axis>
    <pulseEquivalent>
      <add x="3" y="9" z="2"/>
    </pulseEquivalent>
  </usersettings>
  <bestposition>
    <start>
      <add x="0" y="0" value="0"/>
    </start>
    <part>
      <add x="0" y="0" value="1"/>
    </part>
  </bestposition>
</configuration>

撒花撒花

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值