configurationmanager.getsection usage example.
2015-11-06 10:19
309 查看
1.app.config(note that attribute case sensitive!)
2.class (should impliment interface IConfigurationSectionHandler)
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!--configsections must be placed above most! or there may be a "Configuration System Failed to Initialize" error!--> <configSections> <!--section type equals format- "type,assemblyname"--> <section name="CustomConfig" type="SrvListQueryConsole.CustomConfig, SrvListQueryConsole"/> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <!--config "Value" case sensitive--> <CustomConfig> <Name Value="asdf"/> </CustomConfig> </configuration>
2.class (should impliment interface IConfigurationSectionHandler)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using System.Xml; namespace SrvListQueryConsole { class Program { static void Main(string[] args) { var m = ConfigurationManager.GetSection("CustomConfig") as CustomConfig; Console.WriteLine(m.Name); if (args.Length != 2) return; //System.Windows.Forms.Form fm = new ServerListQueryFormDll.SrvListQueryFrm(args[0],args[1]); //fm.ShowDialog(); Console.Read(); } } //mapping! public class CustomConfig : IConfigurationSectionHandler { public string Name { get; private set; } public object Create(object parent, object configContext, XmlNode section) { CustomConfig config = new CustomConfig(); var name = section.SelectSingleNode("Name"); if (name != null && name.Attributes != null) { var attribute = name.Attributes["Value"]; if (attribute != null) config.Name = attribute.Value; } return config; } } }
相关文章推荐
- to improve sqlite performance
- 简单shell
- 淡定淡定淡定
- Android开发艺术探索学习-Activity跳转时的生命周期
- iOS开发宝典:String用法大全
- smarty string_format用法 取小数点后2位
- Java学习笔记(七)——对象
- 深入理解JavaScript系列
- Android design support library---CollapsingAvatarToolbar分析
- MySQL:建表及字段类型选取
- Yii2调用mysql存储过程获取返回值
- 替换linux下的rm命令,并对-rf进行判断
- Android Studio系列教程四--Gradle基础
- 11月计划
- 未定义与 'single' 类型的输入参数相对应的函数 'vl_pr'
- 真的是数据库连接池C3P0的错?
- iTunesConnect构建版本一直处于"正在处理"状态
- 理解Cookie和Session机制
- ARC forbids Objective-C objects in struct
- Redis缓存服务器的配置