• 配置文件读取


    从配置文件中读取内容,可以使用以下方法:

    配置文件以下边为准

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <sectionGroup name="mygroup">
          <section name="mysection"   type="CLRSecondTests.ConfigSection,CLRSecondTests"/>
          <section name="color"  type="System.Configuration.NameValueSectionHandler" />
        </sectionGroup>
      </configSections>
      <mygroup>
        <color>
          <add key="red"   value="#ff0000"/>
          <add key="green" value="#00ff00"/>
          <add key="blue"  value="#0000ff"/>
        </color>
        <mysection  user="test" password="test123">
          <element element1="属性1" element2="属性2"></element>
        </mysection>
      </mygroup>
    </configuration>

    方法一:

      public void Test2()
            {
                NameValueCollection color = (NameValueCollection)ConfigurationManager.GetSection("mygroup/color");
                foreach (String str in color.AllKeys)
                {
                    Console.WriteLine($"str :{color[str]}");
                }
            }


    其中,color配置节的路径要正确,它在组mygroup下,所以,完整的相对路径应该是mygroup/color。 

    然后通过configurationManager获取之后,获取到的是键值对,在配置文件中的类型中要写正确:<section name="color"  type="System.Configuration.NameValueSectionHandler" />

    然后就可以使用了

    方法二:自定义与配置文件结构相同的类型

        public class ConfigSection : ConfigurationSection
        {
            public ConfigSection()
            {
                //
                // TODO: 在此处添加构造函数逻辑
                //
            }
            [ConfigurationProperty("user", DefaultValue = "yanghong", IsRequired = true)]
            public string User
            {
                get { return (string)this["user"]; }
                set { this["user"] = value; }
            }
    
            [ConfigurationProperty("password", DefaultValue = "password", IsRequired = true)]
            public string PassWord
            {
                get { return (string)this["password"]; }
                set { this["password"] = value; }
            }
    
            [ConfigurationProperty("element")]
            public elementinfo Element
            {
                get { return (elementinfo)this["element"]; }
                set { this["element"] = value; }
            }
        }
        public class elementinfo : ConfigurationElement
        {
            public elementinfo() { }
    
    
            [ConfigurationProperty("element1", DefaultValue = "element1", IsRequired = true)]
            public string Element1
            {
                get { return (string)this["element1"]; }
            }
    
            [ConfigurationProperty("element2", DefaultValue = "element2", IsRequired = true)]
            public string Element2
            {
                get { return (string)this["element2"]; }
            }
    
    
        }

    要注意的是:

    自定义类型,类要从ConfigurationSection派生

    然后定义的属性要贴上属性标签,同时,内嵌的类型也要定义完整,

    使用时,可以直接获取配置节,转换为自定义类型:

    public void Test1()
            {
                ConfigSection config = (ConfigSection)ConfigurationManager.GetSection("mygroup/mysection");
                Console.Write($"用户名:{ config.User.ToString() }密码:{config.PassWord} 元素属性:{config.Element.Element1} {config.Element.Element2}");
            }


      

  • 相关阅读:
    [stm32] Systick
    [stm32] GPIO及最小框架
    51单片机-PC数据传输 温度 距离 监控系统设计
    [游戏学习29] Win32 图像处理1
    [51单片机] 串口通讯 简单通信
    [汇编] 闰年计算
    Java常用工具类之ArrayUtil
    常用工具类系列之DateUtil
    SpringBoot 获取当前登录用户IP
    Spring data jpa Specification查询关于日期的范围搜索
  • 原文地址:https://www.cnblogs.com/jams742003/p/13962669.html
Copyright © 2020-2023  润新知