views:

107

answers:

4

I'm trying to use ConfigurationManager.AppSettings.GetValues() to retrieve multiple configuration values for a single key, but I'm always receiving an array of only the last value. My appsettings.config looks like

<add key="mykey" value="A"/>
<add key="mykey" value="B"/>
<add key="mykey" value="C"/>

and I'm trying to access with

ConfigurationManager.AppSettings.GetValues("mykey");

but I'm only getting { "C" }.

Any ideas on how to solve this?

+2  A: 

What you want to do is not possible. You either have to name each key differently, or do something like value="A,B,C" and separate out the different values in code string values = value.split(',').

It will always pick up the value of the key which was last defined (in your example C).

Kevin
+3  A: 

Try

<add key="mykey" value="A,B,C"/>

And

string[] mykey = ConfigurationManager.AppSettings["mykey"].Split(',');
Joel Potter
A: 

Try this one

http://carso-owen.blogspot.com/2007/07/configurationmanager-net.html

Cheers Parminder

Parminder
How is this related to the question?
Heinzi
+1  A: 

The config file treats each line like an assignment, which is why you're only seeing the last line. When it reads the config, it assigns your key the value of "A", then "B", then "C", and since "C" is the last value, it's the one that sticks.

as @Kevin suggests, the best way to do this is probably a value whose contents are a CSV that you can parse apart.

rwmnau