Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app.config multiple values by single key [duplicate]

Tags:

c#

app-config

is it possible to have app.config file like this :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="someKey" value="valueHere"/>
    <add key="anotherKey" value="valueHere"/>
    <add key="listOfValues">
        <value1/>
        ...
        <valueN/>
    </add>
  </appSettings>
</configuration>

I mean, I want to have a key in config file that returns a list of values.How to do it? Think it's pretty easy, but I just can't find any examples

UPD : maybe I should put multiple values separated by semicolon and then just split them?.. But I think it is not very good idea...

like image 632
illegal-immigrant Avatar asked Feb 14 '11 12:02

illegal-immigrant


People also ask

How do I get the value of a key in appsettings?

Now you can use ConfigurationSettings.AppSettings [key] as always, as well as ConfigurationSettings.AppSettings.GetValues (key), which returns a string [] with all the values for that key. You'll find an example in the demo project that uses both.

What is app config in Visual Studio?

When you create a (non-web) .NET Framework application in Visual Studio, an app.config file is added to your project. When you create a class library or a .NET Core project, such a file is not included, although it can be done afterward. In a web project (i.e. ASP.NET) you will use a similar file, the web.config.

How to add new values to a collection from config files?

Using a de-compiler (like the great Lutz Roeder's Reflector ), we'll find the line that adds new values to the collection from the config files looks like this: As you may know, the first form replaces the value if the key already exists in the collection.

How to read appsettings values?

Since ConfigurationSettings.AppSettings, the easiest standard to read app settings uses this handler in a very particular way, developers usually have to create a custom section and read values using ConfigurationSettings.GetConfig and lots of casts. We'll hack AppSettings to make our life easier, and learn some interesting things.


2 Answers

I don't think the standard key/value pair config settings can do it, but with a little more coding you can have all the configuration XML goodness you want with a custom config section.

like image 54
David Avatar answered Oct 23 '22 09:10

David


Don't know if what you're asking is possible. But what I do is I concatenate values using a separator like ";" for instance.

So you have something like:

<add key="runningDays" value="Mon;Tue;Wed;Thu;Fri"/>

Then I split the value string from config using the separator to get the list of possible values for the given key.

like image 14
tzup Avatar answered Oct 23 '22 08:10

tzup