. NET distributed in WEB.CONFGI or app. config for a specified folder

Source: Internet
Author: User

. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.