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()
Best Answer
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 thatand then you can define this section filling it with keys and values:
To get value of a key from this section you have to add
System.Configuration
dll as reference to your project, addusing
and useGetSection
method. Example:Nice thing is that you can easily make groups of sections if you need this:
If you use groups, to access sections you have to access them using
{group name}/{section name}
format: