This article reproduced: http://www.cnblogs.com/lori/archive/2013/04/03/2997617.html
For small projects, configuration information can be configured through appsettings, and if the configuration information is too much, appsettings seems a bit messy, and when the developer calls, is not friendly, the node name is easy to write wrong, at this time, we have several solutions
1 Develop a configuration information persistence class for managing configuration information and providing object-oriented support2 using the. NET configsections, the configuration information is chunked managed, and the entity classes are provided, making it easy for developers to use it
This article is mainly about the second scenario, which consists of the entity class, the entity class factory and the configuration file three parts, see the code:
Entity class Design:
namespace configer{///<summary>// Website Information configuration node/// </summary> public class webconfigsection:configurationsection {//<summary>///Website name/// </summary> [ ConfigurationProperty ("WebName", DefaultValue = "", IsRequired = True, IsKey = False)] public string WebName {
get {return (string) this["WebName"];} set {this["WebName"] = value;} } <summary> ////Website domain///</summary> [ConfigurationProperty ("domain", defaultvalue = "", IsRequired = True, IsKey = False)] public string DoMain { get {return (string) this["DoMain"];} set {this["DoMain"] = value;}}}
Entity factory class design, mainly used to produce entity configuration information
namespace configer{///<summary>/// website configuration information Factory/// </summary> public class Webconfigmanager {//<summary>/// configuration information entities///</summary> public static readonly Webconfigsection Instance = GetSection (); private static Webconfigsection GetSection () { webconfigsection config = configurationmanager.getsection (" Webconfigsection ") as Webconfigsection; if (config = = null) throw new Configurationerrorsexception (); return config;}} }
And finally, the. config file, which has configsections and the specified sections block, it is important to note that the configsections must be in the first position of the configuration
<?xml version= "1.0" encoding= "Utf-8"?><configuration> <configSections> <section Name = "Webconfigsection" type= "configer.webconfigsection, test"/> </configSections> < connectionstrings> <add name= "backgroundentities" connectionstring= "metadata=res://*/model1.csdl|res:/ /*/model1.ssdl|res://*/model1.msl;provider=system.data.sqlclient;provider Connection String="data Source =.\sqlexpress;initial catalog=background;integrated security=true; multipleactiveresultsets=true" "providername=" System.Data.EntityClient "/> </ connectionstrings> <webconfigsection webname= "account Site" domain= "www.zhanzhan.com" /> < appsettings> <add key= "Site" value= "www.zzl.com"/> </appSettings></configuration>
After the implementation of the above three steps, we can call it, hehe
static void Main (string[] args) { Console.WriteLine (System.Configuration.ConfigurationManager.AppSettings ["Site"]); Console.WriteLine (WebConfigManager.Instance.DoMain); Console.WriteLine (WebConfigManager.Instance.WebName); }
The results are as follows:
. NET how to customize config configuration file nodes