. NET, configurationmanager default reads Web. config or app. config However, everything in these two files, feeling too much, also not good management configuration. So refer to the other people's information, wrote an example of their own, examples of the implementation of the E:\App.config file, the file format is as follows<?xml version="1.0"encoding="Utf-8"?><configuration> <configSections> <section name="Books"Type="configurationdemo.bookssection, Configurationdemo"/> </configSections> <books> <book name="123"Author="456"/> </books> </configuration>to achieve such a function, it is necessary to achieve their own configurationsection,configurationelementcollection,configurationelement1. ConfigurationSection Public classbookssection:configurationsection {[ConfigurationProperty ("Books", isrequired =true)] Public stringCategory {Get { return(string)Base["Category"]; } Set { Base["Category"] =value; }} [ConfigurationProperty ("", isdefaultcollection =true)] Publicbookelementcollection Books {Get { return(bookelementcollection)Base[""]; } } }2. ConfigurationElementCollection Public classbookelementcollection:configurationelementcollection {protected Overrideconfigurationelement createnewelement () {return Newbookelement (); } protected Override ObjectGetelementkey (configurationelement element) {return((bookelement) Element). Name; } Public Overrideconfigurationelementcollectiontype CollectionType {Get { returnConfigurationelementcollectiontype.basicmap; } } protected Override stringelementname {Get { return " Book"; } } PublicBookelement This[intIndex] { Get { return(bookelement) baseget (index); } Set { if(Baseget (index)! =NULL) {baseremoveat (index); } baseadd (index, value); } } }3. ConfigurationElement Public classbookelement:configurationelement {[ConfigurationProperty ("name", isrequired =true)] Public stringName {Get { return(string)Base["name"]; } Set { Base["name"] =value; }} [ConfigurationProperty ("author", isrequired =true)] Public DoubleAuthor {Get { return(Double)Base["author"]; } Set { Base["author"] =value; } } }4. 0Config file<?xml version="1.0"encoding="Utf-8"?><configuration> <configSections> <section name="Books"Type="configurationdemo.bookssection, Configurationdemo"/> </configSections> <books> <book name="123"Author="456"/> </books> </configuration>5. 0 readsStatic voidMain (string[] args) { stringConfigpath =@"E:\App.config"; Execonfigurationfilemap Map=NewExeconfigurationfilemap (); Map. Execonfigfilename=Configpath; varConfigManager =configurationmanager.openmappedexeconfiguration (map, Configurationuserlevel.none); if(configmanager.hasfile) {bookssection config= (bookssection) configmanager.getsection ("Books"); Console.WriteLine (config. books[0]. Name); }} Results123Please press any key to continue ...
. NET distributed in WEB.CONFGI or app. config for a specified folder