29

How to have array or list like information in app.config? I want user to be able to put as many IPs as possible (or as needed). My program would just take whatever specified in app.config. How to do this?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="ip" value="x" />
    <add key="ip" value="y" />
    <add key="ip" value="z" />
    </appSettings>
</configuration>



public string ip = ConfigurationManager.AppSettings["ip"];
John Ryann
  • 2,283
  • 11
  • 43
  • 60
  • 1
    write your own configuration section. [Here's](http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx/) a start. [This one](http://www.codeproject.com/Articles/37776/Writing-a-complex-custom-configuration-section) shows you how to loop through them – Jonesopolis May 06 '14 at 15:41
  • @Jonesy Or use the hassle-free `StringCollection` settings type... – Thorsten Dittmar May 06 '14 at 15:51
  • Possible duplicate of [How to get a List collection of values from app.config in WPF?](https://stackoverflow.com/questions/1779117/how-to-get-a-liststring-collection-of-values-from-app-config-in-wpf) – Squazz May 31 '18 at 12:10
  • See my answer here for a way to create your own configuration section: https://stackoverflow.com/a/33544322/1955317 – Squazz May 31 '18 at 12:11

3 Answers3

54

The easiest way would be a comma separated list in your App.config file. Of course you can write your own configuration section, but what is the point of doing that if it is just an array of strings, keep it simple.

<configuration>
  <appSettings>
    <add key="ips" value="z,x,d,e" />
  </appSettings>
</configuration>

public string[] ipArray = ConfigurationManager.AppSettings["ips"].Split(',');
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
milagvoniduak
  • 3,214
  • 1
  • 18
  • 18
20

You can set the type of a setting in the settings designer to StringCollection, which allows you to create a list of strings.

Screenshot

You can later access individual values as Properties.Settings.Default.MyCollection[x].

In the app.config file this looks as follows:

<setting name="MyCollection" serializeAs="Xml">
<value>
    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <string>Value1</string>
        <string>Value2</string>
    </ArrayOfString>
</value>
</setting>
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • When I try and call the setting using Properties.Settings.MyCollection[x], it says An object reference is required for the non-static field, method, or property? any ideas? – Max Carroll Nov 08 '15 at 21:43
  • ahh got it, something like "StringCollection someRegKeys = new Properties.Settings().RegKeys;" – Max Carroll Nov 08 '15 at 21:49
  • Am error in my answer. Must be `Properties.Settings.Default.xyz`. – Thorsten Dittmar Nov 09 '15 at 06:25
  • This is a nicer way to go than using string parsing. In the settings designer, use the '...' on the right of the Values field to edit the collection of strings. It gets converted to XML for you. – bluedog Jul 03 '18 at 21:44
11

In App.config,

<add key="YOURKEY" value="a,b,c"/>

In C#,

STRING ARRAY:

string[] InFormOfStringArray = ConfigurationManager.AppSettings["YOURKEY"].Split(',').Select(s => s.Trim()).ToArray();

LIST :

 List<string> list = new List<string>(InFormOfStringArray);
Divya
  • 373
  • 4
  • 3