c# - ConfigurationErrorsException occured -
i want create custom configuration c# application. app.config likes:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configsections> <section name="location" type ="folderconfigsection.locationconfig, folderconfigsection"/> </configsections> <location> <folders> <add folder ="c:\test1"/> <add folder ="c:\test2" /> <add folder ="c:\test3" /> </folders> </location> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.5" /> </startup> </configuration>
so created 2 classes.
namespace folderconfigsection { public class locationconfig : configurationsection { [configurationproperty("folders")] public string folder { { homecoming base["folder"] string; } set { base["folder"] = value; } } } }
and
namespace folderconfigsection { public class foldercollection : configurationelementcollection { protected override configurationelement createnewelement() { homecoming new folderelement(); } protected override object getelementkey(configurationelement element) { homecoming ((folderelement)element).environment; } } public class folderelement : configurationelement { [configurationproperty("folder", isrequired = true)] public string environment { { homecoming (string)this["folder"]; } set { this["folder"] = value; } } } }
but got exception in program.cs
public class programme { public static void main(string[] args) { locationconfig _locationconfig = (locationconfig)configurationmanager.getsection("location"); string firstfolder = _locationconfig.folder; } }
the exception the section name 'location' reserved <location> sections.
want list folders.
anything wrong?
i'm restating @tim has answered in comments.
do not utilizelocation
section name. reserved <location>
element configuration. utilize other name, such locationconfig
. change type of folder
property in locationconfig
class string
foldercollection
. clarity, should alter property name folder
plural form folders
. to list folders in locationconfig
, need iterate folders
collection property. might want implement indexer on foldercollection
class, indexed access folders
property, . putting in code:
app.config<?xml version="1.0" encoding="utf-8" ?> <configuration> <configsections> <section name="locationconfig" type ="folderconfigsection.locationconfig, folderconfigsection"/> </configsections> <locationconfig> <folders> <add folder ="c:\test1"/> <add folder ="c:\test2" /> <add folder ="c:\test3" /> </folders> </locationconfig> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.5" /> </startup> </configuration>
locationconfig namespace folderconfigsection { using system.configuration; public class locationconfig : configurationsection { // alter property type foldercollection, , // alter property name `folders` clarity [configurationproperty("folders")] public foldercollection folders { { homecoming base["folders"] foldercollection; } // setter property isn't needed here // set { base["folders"] = value; } } } public class foldercollection : configurationelementcollection { protected override configurationelement createnewelement() { homecoming new folderelement(); } protected override object getelementkey(configurationelement element) { homecoming ((folderelement)element).environment; } // indexer public folderelement this[int index] { { homecoming baseget(index) folderelement; } } } public class folderelement : configurationelement { [configurationproperty("folder", isrequired = true)] public string environment { { homecoming (string)this["folder"]; } set { this["folder"] = value; } } } }
program.cs public class programme { static void main(string[] args) { var locationconfig = (locationconfig)configurationmanager.getsection("locationconfig"); foreach (folderelement folder in locationconfig.folders) { // list folders console.writeline(folder.environment); } // first folder using indexer property console.writeline(locationconfig.folders[0].environment); } }
c# .net
No comments:
Post a Comment