App.config自定义节点读取
Posted xxxin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了App.config自定义节点读取相关的知识,希望对你有一定的参考价值。
<?xml version="1.0" encoding="utf-8"?> <configuration> <!--<otherconfig configSource="XmlConfigother.config" />--> <otherconfig a="1111" b="2222" c="333"/> </configuration>
public class OtherConfigInfo : ConfigurationSection { /// <summary> /// 获取配置信息 /// </summary> /// <returns></returns> public static OtherConfigInfo GetConfig() { return GetConfig("otherconfig"); } /// <summary> /// 获取配置信息 /// </summary> /// <param name="sectionName">xml节点名称</param> /// <returns></returns> public static OtherConfigInfo GetConfig(string sectionName) { OtherConfigInfo section = (OtherConfigInfo)ConfigurationManager.GetSection(sectionName); if (section == null) throw new ConfigurationErrorsException("Section " + sectionName + " is not found."); return section; } [ConfigurationProperty("a", IsRequired = false)] public string a { get { return (string)base["a"]; } set { base["a"] = value; } } [ConfigurationProperty("b", IsRequired = false)] public string b { get { return (string)base["b"]; } set { base["b"] = value; } } [ConfigurationProperty("c", IsRequired = false)] public string c { get { return (string)base["c"]; } set { base["c"] = value; } } }
//调用方法 OtherConfigInfo configInfo = OtherConfigInfo.GetConfig(); Console.WriteLine(configInfo.a);
以上是关于App.config自定义节点读取的主要内容,如果未能解决你的问题,请参考以下文章
C# 对 App.config的appSettings节点数据进行加密