使用.NET Framework的配置文件app.config
<configuration><appSettings> <add key="MyConfigString" value="Test Config Data"/></appSettings> </configuration>
public class AppSettingConfig{public string resultValue;public AppSettingConfig(){this.resultValue = ConfigurationManager.AppSettings["MyConfigString"].ToString();}}[TestMethod]public void TestAppSettingConfigNode(){AppSettingConfig appCon = new AppSettingConfig();Assert.AreEqual("Test Config Data", appCon.resultValue);}
没有问题!
我们加个Section来看看如何访问:
<configuration><configSections><sectionGroup name="MySectionGroup"><section name="MyFirstSection" type="System.Configuration.DictionarySectionHandler"/><section name="MySecondSection" type="System.Configuration.DictionarySectionHandler"/></sectionGroup></configSections><MySectionGroup><MyFirstSection><add key="First" value="First Section"/></MyFirstSection><MySecondSection><add key="Second" value="Second Section"/></MySecondSection></MySectionGroup> </configuration>
注意我们在section的type中给出了System.Configuration.DictionarySectionHandler,这也限制了我们在具体的ConfigurationElement中只能使用<add key=”” value=””/>的形式,使得我们GetSection()方法返回的是一个IDictory对象,我们可以根据Key来取得相应的值
public class SectionConfig{public string resultValue;public SectionConfig(){System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);IDictionary dic = ConfigurationManager.GetSection("MySectionGroup/MySecondSection") as IDictionary;this.resultValue = dic["Second"].ToString();}}[TestMethod]public void TestSectionGroupConfigNode(){SectionConfig sc = new SectionConfig();Assert.AreEqual("First Section", sc.resultValue);}
还是没问题。
2. 中级使用
.NET支持对上述提到的configuration类进行扩展,我们可以定义自己的Section。
继承自基类System.Configuration.ConfigurationSection,ConfigurationSection已经提供了索引器用来获取设置数据。
在类中加上ConfigurationProperty属性来定义Section中的Element:
public class CustomSection:System.Configuration.ConfigurationSection{[ConfigurationProperty("sectionId", IsRequired=true, IsKey=true)]public int SectionId { get { return (int)base["sectionId"]; }set { base["sectionId"] = value; }}[ConfigurationProperty("sectionValue", IsRequired = false)]public string SectionValue { get { return base["sectionValue"].ToString(); }set { base["sectionValue"] = value; }}}
操作此Section,我们将其动态加入app.config中,并读出来:
public class CustomSectionBroker {private CustomSection customSection = null;public void InsertCustomSection(){customSection = new CustomSection();customSection.SectionId = 1;customSection.SectionValue = "The First Value";System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);config.Sections.Add("CustomSection", customSection);config.Save(ConfigurationSaveMode.Minimal);}public int GetCustomSectionID(){System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);CustomSection cs = config.GetSection("CustomSection") as CustomSection;return cs.SectionId;}}[TestMethod]public void TestCustomSection(){CustomSectionBroker cb = new CustomSectionBroker();cb.InsertCustomSection();Assert.AreEqual(1, cb.GetCustomSectionID());}
可以看下现在app.config文件的变化:
<configuration><configSections><section name="CustomSection" type="Tonnie.Configuration.Library.CustomSection, Tonnie.Configuration.Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /><sectionGroup name="MySectionGroup"><section name="MyFirstSection" type="System.Configuration.DictionarySectionHandler"/><section name="MySecondSection" type="System.Configuration.DictionarySectionHandler"/></sectionGroup></configSections><CustomSection sectionId="1" sectionValue="The First Value" /><MySectionGroup><MyFirstSection><add key="First" value="First Section"/></MyFirstSection><MySecondSection><add key="Second" value="Second Section"/></MySecondSection></MySectionGroup> </configuration>
public abstract class CustomSectionElementBase:System.Configuration.ConfigurationElement{[ConfigurationProperty("childId", IsRequired=true, IsKey=true)]public int ChildID{get{return (int)base["childId"];}set{base["childId"] = value;}}[ConfigurationProperty("childValue", IsRequired=true)]public string ChildValue{get{return base["childValue"].ToString();}set{base["childValue"] = value;}}}public class CustomSectionElementA:CustomSectionElementBase{public CustomSectionElementA(){base.ChildID = 1;base.ChildValue = "ChildA";}}public class CustomSectionElementB:CustomSectionElementBase{public CustomSectionElementB(){base.ChildID = 2;base.ChildValue = "ChildB";}}
public class CustomSectionWithChildElement:System.Configuration.ConfigurationSection{private const string elementChildA = "childSectionA";private const string elementChildB = "childSectionB";[ConfigurationProperty(elementChildA, IsRequired=true, IsKey=true)]public CustomSectionElementA ChildSectionA { get { return base[elementChildA] as CustomSectionElementA; }set { base[elementChildA] = value; }}[ConfigurationProperty(elementChildB, IsRequired = true)]public CustomSectionElementB ChildSectionB { get { return base[elementChildB] as CustomSectionElementB; }set { base[elementChildB] = value; }}}public class CustomSectionWithChildElementBroker{private CustomSectionWithChildElement customSection = null;public void InsertCustomSection(){customSection = new CustomSectionWithChildElement();customSection.ChildSectionA = new CustomSectionElementA();customSection.ChildSectionB= new CustomSectionElementB();System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);config.Sections.Add("CustomSectionWithChildElement", customSection);config.Save(ConfigurationSaveMode.Minimal);}public int GetCustomSectionChildAID(){System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);CustomSectionWithChildElement cswe = config.GetSection("CustomSectionWithChildElement") as CustomSectionWithChildElement;return cswe.ChildSectionA.ChildID;}}
红色字体就是修改的地方了,将Property改成我们自定义类的形式.测试代码如下:
[TestMethod]public void TestCustomSectionWithChildElement(){CustomSectionWithChildElementBroker cweb = new CustomSectionWithChildElementBroker();cweb.InsertCustomSection();Assert.AreEqual(1, cweb.GetCustomSectionChildAID());}
看看运行后我们的app.config变成什么样子了:
<configuration><configSections><section name="CustomSectionWithChildElement" type="Tonnie.Configuration.Library.CustomSectionWithChildElement, Tonnie.Configuration.Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /><section name="CustomSection" type="Tonnie.Configuration.Library.CustomSection, Tonnie.Configuration.Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /><sectionGroup name="MySectionGroup"><section name="MyFirstSection" type="System.Configuration.DictionarySectionHandler"/><section name="MySecondSection" type="System.Configuration.DictionarySectionHandler"/></sectionGroup></configSections><CustomSectionWithChildElement><childSectionA childId="1" childValue="ChildA" /><childSectionB childId="2" childValue="ChildB" /></CustomSectionWithChildElement><CustomSection sectionId="1" sectionValue="The First Value" /><MySectionGroup><MyFirstSection><add key="First" value="First Section"/></MyFirstSection><MySecondSection><add key="Second" value="Second Section"/></MySecondSection></MySectionGroup> </configuration>
cool,好像完成了我们的要求。
下面为我们的CustomSectionWithChildElement外面再加一层SectionGroup.
public class CustomSectionGroup : System.Configuration.ConfigurationSectionGroup{[ConfigurationProperty("customSectionA", IsRequired = true, IsKey = true)]public CustomSectionWithChildElement SectionA {get { return base.Sections["customSectionA"] as CustomSectionWithChildElement; }set {this.Sections.Add("customSectionA", value);}}}public class CustomSectionGroupWithChildElementBroker{private CustomSectionWithChildElement customSection = null;public void InsertCustomSectionGroup(){customSection = new CustomSectionWithChildElement();customSection.ChildSectionA = new CustomSectionElementA();customSection.ChildSectionB= new CustomSectionElementB();CustomSectionGroup sectionGroup = new CustomSectionGroup();System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);if (config.GetSectionGroup("customSectionGroup") == null)config.SectionGroups.Add("customSectionGroup",sectionGroup);sectionGroup.SectionA = customSection;config.Save(ConfigurationSaveMode.Minimal);}public int GetCustomSectionChildAID(){System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);CustomSectionWithChildElement cswe = config.GetSection("customSectionGroup/customSectionA") as CustomSectionWithChildElement;return cswe.ChildSectionA.ChildID;}}
测试一下:
[TestMethod]public void TestCustomSectionGroupWithChildElement(){CustomSectionGroupWithChildElementBroker cweb = new CustomSectionGroupWithChildElementBroker();cweb.InsertCustomSectionGroup();Assert.AreEqual(1, cweb.GetCustomSectionChildAID());}
没问题,看下现在的app.config,是不是更加结构化了:
<configuration><configSections><sectionGroup name="MySectionGroup"><section name="MyFirstSection" type="System.Configuration.DictionarySectionHandler"/><section name="MySecondSection" type="System.Configuration.DictionarySectionHandler"/></sectionGroup><sectionGroup name="customSectionGroup" type="Tonnie.Configuration.Library.CustomSectionGroup, Tonnie.Configuration.Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" ><section name="customSectionA" type="Tonnie.Configuration.Library.CustomSectionWithChildElement, Tonnie.Configuration.Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /></sectionGroup></configSections><MySectionGroup><MyFirstSection><add key="First" value="First Section"/></MyFirstSection><MySecondSection><add key="Second" value="Second Section"/></MySecondSection></MySectionGroup><customSectionGroup><customSectionA><childSectionA childId="1" childValue="ChildA" /><childSectionB childId="2" childValue="ChildB" /></customSectionA></customSectionGroup> </configuration>
3 高级使用
到目前为止可能大家对app.config有了一定的认识了,我们自己可以不断的去扩展.NET Framework提供给我们的类,从SectionGroup,Section,ElementCollection,Element 从上自下的一级一级的组装成符合工程化项目配置文件需要的形式。当遇到可能配置元素的类型属性差不多时,可以抽象出一个base类来。比如可以抽象出Section这一层面的base类,或者ElementCollection,Element这一层的抽象类(可以是抽象的泛型类)来。同时增加泛型来更好的支持扩展。
使用.NET Framework的配置文件app.config相关推荐
- 一步一步教你玩转.NET Framework的配置文件app.config (转载)
一步一步教你玩转.NET Framework的配置文件app.config 在一般的项目中,为了使你的代码更加灵活,更方便调整,减少不必要的hard code,我们都在config中添加许多配置信息, ...
- 类项目中的配置文件app.config在打包安装后的信息获取的问题
在一个项目中碰到这样的一个问题,做一个WORD插件,功能在类库项目中实现了,配置信息存在类库项目的配置文件app.config中,在进行打包后,获取的配置文件中的DocType节点信息时,使用以下方法 ...
- C#中怎样获取默认配置文件App.config中配置的键值对内容
场景 在新建一个程序后,项目中会有一个默认配置文件App.config 一般会将一些配置文件信息,比如连接数据库的字符串等信息存在此配置文件中. 怎样在代码中获取自己配置的键值对信息. 注: 博客主页 ...
- 配置文件app.config
无论对于客户端程序还是web应用程序,配置文件的作用不言而喻,现总结用法如下: 1. 创建配置节类 必须创建继承自ConfigurationSection的对象才能进行配置数据读写操作,Configu ...
- winform程序读取和改写配置文件App.config元素的值
在开发Web项目的时候,会有一个配置文件Web.config,用来存放一些全局的变量,如连接数据库用的字符串.相应的,在开发winform程序时,也有一个配置文件,它就是App.config,这个文件 ...
- C#读取配置文件app.config
应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序. 配置文件的根节点是configuration ...
- (原)举例说明C#自定义配置文件app.config及如何读取配置内容(在app.config加入自定义的XML节点)?...
App.config文件如下: <?xml version="1.0" encoding="utf-8" ?> <configuration& ...
- 类库如何读取配置文件(app.config)?
我们知道,在WinForm或者是Console应用程序中,我们可以使用app.config文件,做为程序的配置信息的储存.然后我们使用System.Configuration类进行调用,读取或更新等. ...
- 配置文件App.config 生成和调用的两种方式
方式一: App.config //app.config<?xml version="1.0" encoding="utf-8"?> <con ...
最新文章
- ali arthas 火焰图_带你上手阿里开源的 Java 诊断利器:Arthas
- eclipse创建android工程,在eclipse创建android 工程
- iOS开发学习之MapKit - 获得在MapView(地图)中显示多个标记的区域(MKCoordinateRegion)...
- 自己写的.Net(C#)代码×××
- 一主多备几影响 oracle,Oracle 10g 一主多备的搭建技巧
- mfc 学习的第二天
- win10 linux子系统开启桌面显示
- GitHub 简单教程
- 常用的机器学习与深度学习算法简介
- CocosCreator角色动态更换武器实现
- 【机器学习】金融风控评分卡建模全流程!
- python语言的变量_自兴人工智能------Python语言的变量认识及操作
- 招行零售金融3.0数字化转型实践
- Java-翁恺进阶 城堡游戏的前世今生
- 关于Unicode字符集,将char数组转化成LPCTSTR
- python猜单词游戏_17.Python猜单词游戏
- 提前还房贷需三思 专家建议两类人可提前还款
- 中国能出现一家 Zoom 吗?
- 飞行计算机人机工程,人机工程学ppt.ppt
- vue登录获取验证码
热门文章
- 铁乐学Python_day09_函数
- 四大组件之Activity
- P2658 汽车拉力比赛
- OC 观察者模式(通知中心,KVO)
- 准备 LVM Volume Provider - 每天5分钟玩转 OpenStack(49)
- Vue 组件 style scoped
- python的migratetodb_Python的Flask框架中使用Flask-Migrate扩展迁移数据库的教程
- flutter 轮播图动态加载网络图片
- 在windows server 2008 R2上运行disk cleanup
- nginx日志定期归档脚本