none
Duplicate keys in appSettings in App.Config

    Question

  • If I have the following xml in my App.Config

      <appSettings>
        <add key="Key" value="Value1" />
        <add key="Key" value="Value2" />
      </appSettings>

    then calling ConfigurationManager.AppSettings.GetValues("Key") returns an array of strings with only one entry -- the last entry in in the appSettings ("Value2").

    There was a useful article written on how to get this to work on CodeProject (http://www.codeproject.com/dotnet/namevaluemultiple.asp), but that doesn't seem to work with .net 2.0 (I get a run-time exception that the appsettings section can't be modified).

    Does anyone know of a sample somewhere on how to get this to work in .net 2.0?

    Saturday, March 24, 2007 4:12 PM

Answers

  • I get around this these days by doing:

      <appSettings>
        <add key="Key" value="Value1|Value2" />
      </appSettings>

    Or whatever else makes sense as a delimiter instead of | . Then I just use String.Split() to break down the value string into an array of strings.
    /\/\}{
    • Marked as answer by MuscleHead Tuesday, July 15, 2008 2:11 AM
    Monday, July 07, 2008 1:49 AM

All replies

  • I get around this these days by doing:

      <appSettings>
        <add key="Key" value="Value1|Value2" />
      </appSettings>

    Or whatever else makes sense as a delimiter instead of | . Then I just use String.Split() to break down the value string into an array of strings.
    /\/\}{
    • Marked as answer by MuscleHead Tuesday, July 15, 2008 2:11 AM
    Monday, July 07, 2008 1:49 AM
  • Ran into same thing, this is clearly A BUG not a feature. 

    The function GetValues says it returns a string array and compiles as such. 

    We're not getting an array, just a scalar, so it's a bug. 

    Faking an array by manually putting delimiters in a single string and doing a split, is still just getting a single string from the function. 

    It's a bug.

    Microsoft, please fix.

    Tuesday, February 16, 2010 7:57 PM
  • And its a bug that's been around for donkeys years.

     

    Microsoft please fix

    Thursday, March 17, 2011 11:42 AM
  • It's STILL present in .net 4.5.

    PLEASE FIX THIS

    Tuesday, January 28, 2014 5:23 AM