Config file:
<?XML version= "1.0" encoding= "Utf-8"?><Configuration> <configsections> < Sectionname= "Filedepend"type= "Testconsole.filedepend,testconsole"/> </configsections> <Filedepend> <RootDirPath= "c \"></RootDir> < Public> <elementfile= "/1.txt"></element> <elementfile= "/2.txt"></element> </ Public> <Modules> <Modulename= "Legend"> <elementfile= "/3.txt"></element> <elementfile= "/4.txt"></element> </Module> <Modulename= "Bookmark"> <elementfile= "/5.txt"></element> <elementfile= "/6.txt"></element> </Module> </Modules> </Filedepend> <Startup> <supportedruntimeversion= "v4.0"SKU=". netframework,version=v4.6 " /> </Startup></Configuration>
FileDepend.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.Linq;namespacetestconsole{ Public classfiledepend:configurationsection {[ConfigurationProperty ("RootDir")] PrivateRootdirelement _rootdir = (rootdirelement)Base["RootDir"]; [ConfigurationProperty (" Public")] PrivateFilescollection publicfilescollection = ((filescollection) (Base[" Public"])); Public stringRootDir =_rootdir.name; [ConfigurationProperty ("Modules")] PublicModulescollection modulescollection = ((modulescollection) (Base["Modules"])); Publicienumerable<string> publicfiles = fromFileelement VinchPublicfilescollectionSelectV.name; } Public classrootdirelement:configurationelement {[ConfigurationProperty ("Path", DefaultValue ="", IsKey =true, isrequired =true)] Public stringName = (string)Base["Path"]; } Public classfileelement:configurationelement {[ConfigurationProperty ("file", DefaultValue ="", IsKey =true, isrequired =true)] Public stringName = (string)Base["file"]; } Public classmoduleelement:configurationelement {[ConfigurationProperty ("name", DefaultValue ="", IsKey =true, isrequired =true)] Public stringName {Get{return(string)Base["name"]; } Set{Base["name"] =value;} } [ConfigurationProperty ("", isdefaultcollection =true)] PrivateFilescollection Element = (filescollection)Base[""]; Publicienumerable<string> Files = fromFileelement fileinchElementSelectfile. Name; } [Configurationcollection (typeof(moduleelement))] Public classfilescollection:configurationelementcollection {Internal Const stringPropertyName ="element"; Public OverrideConfigurationelementcollectiontype CollectionType =configurationelementcollectiontype.basicmapalternate; protected Override stringElementName =PropertyName; protected Override BOOLIselementname (stringelementname) { returnelementname.equals (PropertyName, Stringcomparison.invariantcultureignorecase); } Public Override BOOLisreadonly () {return false; } protected Overrideconfigurationelement createnewelement () {return Newfileelement (); } protected Override ObjectGetelementkey (configurationelement element) {return( (Fileelement) (Element)). Name; } PublicFileelement This[intIDX] =(fileelement) baseget (IDX); Public NewFileelement This[stringIDX] =(fileelement) baseget (IDX); } [Configurationcollection (typeof(moduleelement))] Public classmodulescollection:configurationelementcollection {Internal Const stringPropertyName ="Module"; Public OverrideConfigurationelementcollectiontype CollectionType =configurationelementcollectiontype.basicmapalternate; protected Override stringElementName =PropertyName; protected Override BOOLIselementname (stringelementname) { returnelementname.equals (PropertyName, Stringcomparison.invariantcultureignorecase); } Public Override BOOLisreadonly () {return false; } protected Overrideconfigurationelement createnewelement () {return Newmoduleelement (); } protected Override ObjectGetelementkey (configurationelement element) {return( (Moduleelement) (Element)). Name; } PublicModuleelement This[intIDX] =(moduleelement) baseget (IDX); Public NewModuleelement This[stringIDX] =(moduleelement) baseget (IDX); }}
Run
Static voidMain (string[] args) { varv = configurationmanager.getsection ("Filedepend") asFiledepend; varRootDir =V.rootdir; varPublicfiles =V.publicfiles; varLegendfiles = v.modulescollection["legend"]. Files; Console.WriteLine (RootDir); Publicfiles.tolist (). ForEach (Console.WriteLine); Legendfiles.tolist (). ForEach (Console.WriteLine); Console.ReadLine (); }
Create a custom configsection in Web. config or app. config file