Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sub appsettings in the appsetting node c#

Tags:

c#

app-config

I am using the app.config file that is created with a console application and I can read the val1 of the key1 using the ConfigurationSettings.AppSettings["key1"].ToString()

<configuration>       <startup>          <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />     </startup>       <appSettings>         <add key="key1" value="val1" />         <add key="key2" value="val2" />       </appSettings>  </configuration> 

but I have too many keys and values that I want to make them categorized.

I found something that is difficult to use in my application since I want to access the keys in a similar way to the above one

Showing all nodes and can't read a node without getting all the nodes

for example what I want to do:

<appSettings>     <Section1>         <add key="key1" value="val1" />     </Section1>     <Section2>         <add key="key1" value="val1" />     <Section2> </appSettings> 

and if there is a way to access it using ConfigurationSettings.AppSettings["Section1"].["key1"].ToString()

like image 996
Tony Germanos Avatar asked Feb 11 '13 12:02

Tony Germanos


People also ask

What is the use of ConfigurationManager AppSettings?

Retrieves a specified configuration section for the current application's default configuration.

Where do I put AppSettings in web config?

Locate the web. config file in the root directory of your application (or create one if it does not already exist). Add an <appSettings> element. Add <add> child elements along with key / value pairs to the <appSettings> element as required.

What is the AppSettings section in the web config file?

The <appSettings> element stores custom application configuration information, such as database connection strings, file paths, XML Web service URLs, or any other custom configuration information for an application.


1 Answers

You can add custom sections in app.config without writing additional code. All you have to do is "declaring" new section in configSections node like that

<configSections>       <section name="genericAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />   </configSections> 

and then you can define this section filling it with keys and values:

  <genericAppSettings>       <add key="testkey" value="generic" />       <add key="another" value="testvalue" />   </genericAppSettings> 

To get value of a key from this section you have to add System.Configuration dll as reference to your project, add using and use GetSection method. Example:

using System.Collections.Specialized; using System.Configuration;  namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             NameValueCollection test = (NameValueCollection)ConfigurationManager.GetSection("genericAppSettings");              string a = test["another"];         }     } } 

Nice thing is that you can easily make groups of sections if you need this:

<configSections>     <sectionGroup name="customAppSettingsGroup">       <section name="genericAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />  // another sections     </sectionGroup> </configSections>    <customAppSettingsGroup>     <genericAppSettings>       <add key="testkey" value="generic" />       <add key="another" value="testvalue" />     </genericAppSettings>     // another sections   </customAppSettingsGroup> 

If you use groups, to access sections you have to access them using {group name}/{section name} format:

NameValueCollection test = (NameValueCollection)ConfigurationManager.GetSection("customAppSettingsGroup/genericAppSettings"); 
like image 154
Konrad Gadzina Avatar answered Sep 20 '22 23:09

Konrad Gadzina