-3

Currently i have a list of states that determine whether a link is visible or not on my site. I am trying to find a way to move this list into my web.config file so as the list of states grows i do not have to republish in order to make a change.

string[] statesneeded = new string[] {"AZ","GA","IA", "TN", "SC", "KS", "MI" ,"NC", "UT"};

How would i reference this list in my web config and then call it in C#? Any help is appreciated. Thank you.

Leonardo Wildt
  • 2,529
  • 5
  • 27
  • 56
  • Is this list user based or has it application scope? If the former you could [use the settings](http://stackoverflow.com/a/453230/284240), especially [the `System.Collections.Specialized.StringCollection`](http://stackoverflow.com/a/10419321/284240). – Tim Schmelter Jul 06 '15 at 14:14
  • 1
    [C Sharp Removals](http://www.yellowpages.ca/bus/Ontario/C-Sharp-Removal/6835240.html), for all your moving needs... – musefan Jul 06 '15 at 14:17
  • Thanks for all the help found that i had a settings class in my project which held all info so as not to use configuration manager everywhere. – Leonardo Wildt Jul 06 '15 at 14:43

1 Answers1

2

You could make it a comma-separated string and store it in the appSettings section:

<add key="statesUsedForFooFunctionality" value="AZ,GA,IA, TN, SC, KS, MI ,NC, UT" />

And split it into an array:

string statesFromConfig = ConfigurationManager.AppSettings["statesUsedForFooFunctionality"];
if (string.IsNullOrWhiteSpace("statesFromConfig"))
{
    throw new ConfigurationErrorsException("Required AppSettings key 'statesFromConfig' is missing.");
}

string[] statesneeded = statesFromConfig.Split(',');

But this is also explained in the duplicate How to get a List<string> collection of values from app.config in WPF?.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272