app config - sub appsettings in the appsetting node c# -
i using app.config file created console application , can read val1 of key1 using configurationsettings.appsettings["key1"].tostring()
<startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.0" />
</startup>
`
<add key="key1" value="val1" /> <add key="key2" value="val2" />
</appsettings> </configuration>
but have many keys , values want create them categorized.
i found hard utilize in application since want access keys in similar way above one
showing nodes , can't read node without getting nodes
for illustration want do:
<appsettings> <section1> <add key="key1" value="val1" /> </section1> <section2> <add key="key1" value="val1" /> <section2> </appsettings>
and if there way access using configurationsettings.appsettings["section1"].["key1"].tostring()
you can add together custom sections in app.config without writing additional code. have "declaring" new section in configsections
node that
<configsections> <section name="genericappsettings" type="system.configuration.appsettingssection, system.configuration, version=2.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a" /> </configsections>
and can define section filling keys , values:
<genericappsettings> <add key="testkey" value="generic" /> <add key="another" value="testvalue" /> </genericappsettings>
to value of key section have add together system.configuration
dll reference project, add together using
, utilize getsection
method. example:
using system.collections.specialized; using system.configuration; namespace consoleapplication1 { class programme { static void main(string[] args) { namevaluecollection test = (namevaluecollection)configurationmanager.getsection("genericappsettings"); string = test["another"]; } } }
nice thing can create groups of sections if need this:
<configsections> <sectiongroup name="customappsettingsgroup"> <section name="genericappsettings" type="system.configuration.appsettingssection, system.configuration, version=2.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a" /> // sections </sectiongroup> </configsections> <customappsettingsgroup> <genericappsettings> <add key="testkey" value="generic" /> <add key="another" value="testvalue" /> </genericappsettings> // sections </customappsettingsgroup>
if utilize groups, access sections have access them using {group name}/{section name}
format:
namevaluecollection test = (namevaluecollection)configurationmanager.getsection("customappsettingsgroup/genericappsettings");
c# app-config
No comments:
Post a Comment