Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store String Array In appSettings?

I'd like to store a one dimensional string array as an entry in my appSettings. I can't simply separate elements with , or | because the elements themselves could contain those characters.

I was thinking of storing the array as JSON then deserializing it using the JavaScriptSerializer.

Is there a "right" / better way to do this?

(My JSON idea feels kinda hacky)

like image 740
Greg Avatar asked May 02 '12 17:05

Greg


2 Answers

You could use the AppSettings with a System.Collections.Specialized.StringCollection.

var myStringCollection = Properties.Settings.Default.MyCollection; foreach (String value in myStringCollection) {      // do something } 

Each value is separated by a new line.

Here's a screenshot (german IDE but it might be helpful anyway)

enter image description here

like image 98
Tim Schmelter Avatar answered Sep 17 '22 14:09

Tim Schmelter


For strings it is easy, simply add the following to your web.config file:

<add key="myStringArray" value="fred,Jim,Alan" /> 

and then you can retrieve the value into an array as follows:

var myArray = ConfigurationManager.AppSettings["myStringArray"].Split(','); 
like image 42
Ed Homer Avatar answered Sep 20 '22 14:09

Ed Homer