Using multiple values ​​for one key in appSettings

I will learn how to use the configuration files, and I ran into some problems that I hope that someone here can give me some tips. It doesn't matter if my XML files are or not, but most of the examples I read use them, and Im is all that makes my life easier.

the problem I am facing is that the appSettings file seems to be configured to accept only one value for one key, and I would like to have something similar to:

<key="Machine List" value="Server105" /> <key="Machine List" value="Server230" /> 

Ive found hack here , but it was written over 6 years ago, and I did not know if there is a better way.

Again, it doesn't matter if it's XML, a flat file, etc. I'm just trying to learn how to use configuration files instead of hard coding values ​​directly in the application.

Thank you for your help.

+7
c # configuration-files
source share
4 answers

if you really need to store several machines under the key, it would be more appropriate:

 <key="Machine List" value="Server105,Server230" /> 

with a separator that is a symbol of your choice.

+10
source share

An alternative to login attributes would be to add child nodes to the node setup:

  <setting key="Machine List"> <value>Server105</value> <value>Server230</value> </setting> 

This way you do not need string manipulations to extract different values.

+10
source share

You can use configuration sections where you can define your own configuration. Just add

 <configSections> <sectionGroup name="MyConfiguration"> <section name="MyQuery" type="namespace.QueryConfigurationSection" allowLocation="true" allowDefinition="Everywhere"/> </sectionGroup> </configSections> 

after <configuration> , and you can add your own section right after appsetting

 </appSettings> <!-- custom query configuration --> <MyConfiguration> <MyQuery> <Query1> </Query1> <Query2> </Query2> 

For reading you need to create some classes

 /// <summary> /// Creates a custom configuration section inside web.config /// </summary> public class QueryConfigurationSection : ConfigurationSection { //query 2 [ConfigurationProperty("Query1")] public QueryElement1 Query1 { get { return this["Query1"] as QueryElement1; } } //query 2 [ConfigurationProperty("Query2")] public QueryElement2 Query2 { get { return this["Query2"] as QueryElement2; } } } public class QueryElement1 : ConfigurationElement { public string Value { get; private set; } protected override void DeserializeElement(XmlReader reader, bool s) { Value = reader.ReadElementContentAs(typeof(string), null) as string; } } public class QueryElement2 : ConfigurationElement { public string Value { get; private set; } protected override void DeserializeElement(XmlReader reader, bool s) { Value = reader.ReadElementContentAs(typeof(string), null) as string; } } 

An overridden deserialized element deserializes Xml (internally) QueryElement1 and 2.

To read the values ​​from the main application, you just need to call the following:

  //calling my query config QueryConfigurationSection wconfig = (QueryConfigurationSection)ConfigurationManager.GetSection("MyConfiguration/MyQuery"); string _query1 = wconfig.Query1.Value; string _query2 = wconfig.Query2.Value; 
+1
source share

Maybe you should rethink your design. I would just put the list you want in a different file, and not in the config. You can make a delimited string, but if the list is long, it will be difficult to handle it. You can simply put it in a text file or XML / JSON file. Here is some code that could be a good place to start.

 public static class MyClass { private static string _path = ConfigurationManager.AppSettings["FilePath"]; private static List<string> _list; static MyClass() { _list = new List<string>(); foreach (string l in File.ReadAllLines(_path)) _list.Add(l); } public static List<string> GetList() { return _list; } } 

I made it a static class, so it will only read from the file once, and not every time you need to get information from it.

It can also be useful to put in a database if you need more functionality. But for a small read-only thing, this will work better than a delimited string for longer values.

+1
source share

All Articles