转载

https://www.cnblogs.com/lxshwyan/p/10828305.html

如果使用了configSection节点,则configSection必须位于根节点的第0个。App.config中代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="CustomerSingleConfig" type="ConfiguraDemo.CustomerSingleConfig,ConfiguraDemo"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<CustomerSingleConfig PlatChName="监控平台系统" PlatEnName="Monitoring platform system"></CustomerSingleConfig>
</configuration>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConfiguraDemo
{
public class CustomerSingleConfig : ConfigurationSection
{
/// <summary>
/// 获取配置信息
/// </summary>
/// <returns></returns>
public static CustomerSingleConfig GetConfig()
{
return GetConfig("CustomerSingleConfig");
}
private static CustomerSingleConfig GetConfig(string sectionName)
{
try
{
CustomerSingleConfig section = (CustomerSingleConfig)ConfigurationManager.GetSection(sectionName);
if (section == null)
throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
return section;
}
catch (Exception ex)
{
throw new ConfigurationErrorsException("Section " + ex.Message);
}
} /// <summary>
/// 平台中文名称
/// </summary>
[ConfigurationProperty("PlatChName", DefaultValue = "", IsRequired = true, IsKey = false)]
public string PlatChineseName
{
get { return (string)this["PlatChName"]; }
set { this["PlatChName"] = value; }
}
/// <summary>
/// 平台英文名称
/// </summary>
[ConfigurationProperty("PlatEnName", DefaultValue = "", IsRequired = true, IsKey = false)]
public string PlatEnglishName
{
get { return (string)this["PlatEnName"]; }
set { this["PlatEnName"] = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConfiguraDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("---------------------单级配置节点测试-----------------");
Console.WriteLine("PlatChName:" + CustomerSingleConfig.GetConfig().PlatChineseName);
Console.WriteLine("PlatEnName:" + CustomerSingleConfig.GetConfig().PlatEnglishName);
Console.ReadKey();
}
}
}

输出结果

---------------------单级配置节点测试-----------------
PlatChName:监控平台系统
PlatEnName:Monitoring platform system

继承ConfigurationSection需要添加System.Configuration引用

最新文章

  1. 老生长谈:css实现右侧固定宽度,左侧宽度自适应
  2. sql数据库表被锁,无法查询
  3. Wince下sqlce数据库开发(一)
  4. WP8图片缩放功能实现
  5. 初试“七牛云”--零基础运用七牛云配合UEditor实现图片的上传和浏览(.NET篇)
  6. js实现input输入框只能输入数字的功能(完美测试通过)
  7. Apache Kafka源码分析 - kafka controller
  8. JeeSite是基于多个优秀的开源项目,高度整合封装而成的高效,高性能,强安全性的 开源 Java EE快速开发平台
  9. spring beans源码解读之--Bean的定义及包装
  10. Mongo数据模型
  11. 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证
  12. Java中Overload和Override的区别
  13. (转)eclipse 启动参数介绍(如添加插件时,如果不显示,则使用eclipse -clean启动)
  14. AngularJS2基本构造
  15. [网络流]BZOJ4657 最小割约束
  16. 关于ajax用户名验证和jquery实现简单表单验证
  17. Python函数基础--def及return语句地操作
  18. Devexpress 百分号显示格式
  19. Linux的CPU相关知识
  20. cmder中文显示相关问题解决方案(1.3以上版本)

热门文章

  1. 牛客练习赛99—C
  2. JDBC的学习 3-1
  3. 第六十六篇:Vue的watch侦听器
  4. FFT/NTT 学习笔记
  5. picoCTF:keygenme-py 解题思路
  6. [报错]-RuntimeError: Input type (torch.cuda.HalfTensor) and weight type (torch.cuda.FloatTensor) should be the same
  7. 第六章:Django 综合篇 - 9:序列化 serializers
  8. Elasticsearch:分布式计分
  9. 关于Elasticsearch使用java的说明
  10. KVM命令参数