Reputation: 2770
I'm learning about how to use config files and I ran into some problems that I'm hoping someone here can give me some advice. It doesn't matter if my files are XML or not but the majority of examples I have read are using them and Im all for anything that makes my life easier.
the problem Im running into is that the appSettings file seems to be setup to only accept 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 a hack here but it was written over 6 years ago and I didn't know if there was a better way.
Again, it doesnt matter if this is XML, a flat file, etc.... Im just trying to learn how to use config files instead of hard coding values directly into the app.
Thanks for your help.
Upvotes: 8
Views: 15666
Reputation: 355
Maybe you should rethink your design. I would just put the list you want in another file and not the config. You could do a delimited string but then if the list got long it would be hard to manage it. You could just put it in a text file or an XML/JSON file. Here is some code that might 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 would only read from the file once and not everytime you need to get information from it.
This might also be a good thing to put in a database if you need more functionality. But for a small read-only kind of thing, this will work better than a delimited string for longer values.
Upvotes: 1
Reputation: 4255
You can make use of 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 the <configuration>
and you can add your custom section just after the appsetting
</appSettings>
<!-- custom query configuration -->
<MyConfiguration>
<MyQuery>
<Query1> </Query1>
<Query2> </Query2>
To read you need to create few 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;
}
}
The overridden DeserializedElement will deserialize the Xml(inside) the QueryElement1 & 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;
Upvotes: 1
Reputation: 41509
An alternative to entry attributes would be to add child nodes to your setting node:
<setting key="Machine List">
<value>Server105</value>
<value>Server230</value>
</setting>
This way you don't need string manipulations to extract the different values.
Upvotes: 10
Reputation: 9216
if you really need to store multiple machines under the key, it would be more appropriate to do:
<key="Machine List" value="Server105,Server230" />
with the delimiter being a character of your choosing.
Upvotes: 10