Sunday, 15 July 2012

c# - How to get all sections by name in the sectionGroup applicationSettings in .Net 2.0 -



c# - How to get all sections by name in the sectionGroup applicationSettings in .Net 2.0 -

here's thought had:

i want little executable have app.config file multiple sections situated under sectiongroup "applicationsettings" (not "appsettings", don't need write file). each section have name corresponding module should loaded if set.

here's example:

<configuration> <configsections> <sectiongroup name="applicationsettings" type="system.configuration.applicationsettingsgroup, system, version=2.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" > <section name="executable" type="system.configuration.clientsettingssection, system, version=2.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" requirepermission="false" /> <section name="firstmodule" type="system.configuration.clientsettingssection, system, version=2.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" requirepermission="false" /> </sectiongroup> </configsections> <applicationsettings> <executable> <setting name="myfirstsetting" serializeas="string"> <value>my awesome feature setting</value> </setting> </executable> <firstmodule path="path modules assembly"> <setting name="importantsettingtothemodule" serializeas="string"> <value>some of import string</value> </setting> </firstmodule> </applicationsettings> </configuration>

now if define firstmodule section, want application load assembly. if section not defined, module should not loaded. should true not 1 module not yet defined number of them.

so need find out defined sections @ runtime. how that?

in add-on want become portable executable (= has run on mono well) backwards compatible .net 2.0.

it might interesting have @ project on github (currently @ this commit).

take @ configurationmanager.openexeconfiguration function load in configuration file.

then on system.configuration.configuration class you'll configurationmanager.openexeconfiguration you'll want @ sectiongroups property. that'll homecoming configurationsectiongroupcollection in you'll find applicationsettings section.

in configurationsectiongroupcollection there sections property contains executable , firstmodule configurationsection objects.

var config = configurationmanager.openexeconfiguration(pathtoexecutable); var applicationsettingsectiongroup = config.sectiongroups["applicationsettings"]; var executablesection = applicationsettingsectiongroup.sections["executable"]; var firstmodulesection = applicationsettingsectiongroup.sections["firstmodule"];

you want check null after getting configurationsectiongroupcollection object or configurationsection objects. if null don't exist in configuraiton file.

you can sections using configurationmanager.getsection

var executablesection = (clientsettingssection)configurationmanager .getsection("applicationsettings/executable"); var firstmodulesection = (clientsettingssection)configurationmanager .getsection("applicationsettings/firstmodule");

again, if objects null don't exist in configuration file.

to list of section names , groups do:

var config = configurationmanager.openexeconfiguration(pathtoexecutable); var names = new list<string>(); foreach (configurationsectiongroup csg in config.sectiongroups) names.addrange(getnames(csg)); foreach (configurationsection cs in config.sections) names.add(cs.sectioninformation.sectionname); private static list<string> getnames(configurationsectiongroup configsectiongroup) { var names = new list<string>(); foreach (configurationsectiongroup csg in configsectiongroup.sectiongroups) names.addrange(getnames(csg)); foreach(configurationsection cs in configsectiongroup.sections) names.add(configsectiongroup.sectiongroupname + "/" + cs.sectioninformation.sectionname); homecoming names; }

c# configuration mono .net-2.0 configurationsection

No comments:

Post a Comment