Getting list of key values from app config with same name
-
30-06-2021 - |
Question
I have follwing in app.config file:
<appSettings>
<add key="Name" value="Office"/>
...
<add key="Name" value="HotSpot"/>
...
<add key="Name" value="Home"/>
</appSettings>
I tried
ConfigurationManager.AppSettings["Name"]
But it only gives me one Value? How can i get list of all values? I am using c# 3.5. Is there lambda expression or something i can use to get that?
Solution
You can only use one key per value, so this approach will not work.
There are two alternate approaches I can think of:
Use a single key with a delimiter, and retrieve with
ConfigurationManager.AppSettings["Name"].Split(new [] { "," });
.<add key="Name" value="Office,Hotspot,Home" />
Use a custom section to create a section that can contain your array of strings.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow