-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.

3
  • Is this list user based or has it application scope? If the former you could use the settings, especially the System.Collections.Specialized.StringCollection. Commented Jul 6, 2015 at 14:14
  • 1
    C Sharp Removals, for all your moving needs... Commented Jul 6, 2015 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. Commented Jul 6, 2015 at 14:43

1 Answer 1

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?.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.