zoukankan      html  css  js  c++  java
  • 配置configSections

    自定义配置config

    app.config
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <configSections>
    <section name="Jxb1" type="WindowsFormsApplication1.Jxb1,WindowsFormsApplication1"/>
    <section name="Jxb2" type="WindowsFormsApplication1.Jxb2,WindowsFormsApplication1"/>
    </configSections>
    <Jxb1 name="jxb" url="www.51joys.com">
    </Jxb1>
    <Jxb2>
    <users username="BoBo" age="24" sex="Male"></users>
    <!--<users username="Dongzi" age="23" sex="female"></users>-->
    </Jxb2>

    </configuration>
    处理类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;

    namespace WindowsFormsApplication1
    {
    public class Jxb1 : ConfigurationSection
    {
    [ConfigurationProperty("name", IsRequired = true)] //此特性必须申明
    public string User
    {
    get { return this["name"].ToString(); }
    }
    [ConfigurationProperty("url", IsRequired = true)]
    public string url
    {
    get { return this["url"].ToString(); }
    }
    }
    public class Jxb2 : ConfigurationSection
    {
    [ConfigurationProperty("users", IsRequired = true)]
    public MySection Users
    {
    get { return (MySection)this["users"]; }
    }



    }
    public class MySection : ConfigurationElement
    {
    [ConfigurationProperty("username", IsRequired = true)]
    public string UserName
    {
    get { return this["username"].ToString(); }
    }
    [ConfigurationProperty("age", IsRequired = true)]
    public int Age
    {
    get { return Convert.ToInt32(this["age"]); }
    }
    [ConfigurationProperty("sex", IsRequired = true)]
    public string Sex
    {
    get { return this["sex"].ToString(); }
    }
    }
    }
    调用实例
       private void button1_Click(object sender, EventArgs e)
    {
    Jxb1 jxb = (Jxb1)ConfigurationManager.GetSection("Jxb1");
    textBox1.Text = jxb.User;
    textBox2.Text = jxb.url;
    }

    private void button2_Click(object sender, EventArgs e)
    {
    Jxb2 jxb = (Jxb2)ConfigurationManager.GetSection("Jxb2");
    textBox3.Text = jxb.Users.UserName;
    textBox4.Text = jxb.Users.Age.ToString();
    textBox5.Text = jxb.Users.Sex;
    }




  • 相关阅读:
    【BZOJ】4636: 蒟蒻的数列
    BZOJ1878 [SDOI2009]HH的项链
    【网络流24题----02】太空飞行计划
    【网络流24题----03】Air Raid最小路径覆盖
    【网络流24题----01】飞行员配对方案问题
    素数判定(米勒测试定理-费马小定理+快速乘)
    一堆模板(丑陋0.0)------数据结构
    丑数(USACO)
    NOI[2001]食物链
    关于Tarjan(2)
  • 原文地址:https://www.cnblogs.com/Loofah/p/2434854.html
Copyright © 2011-2022 走看看