If you want to use System.Configuration to open your custom files, you may want to check on this post: Loading custom configuration files. Oliver nails it in a very straightforward way.
Since you want to read parameters passed to your application via the command line, you may want to pay a visit to this MSDN post: Command Line Parameters Tutorial.
If you'd rather use a custom approach, there's a few ways you can accomplish this. One possibility is to implement a loader class, and consume your custom configuration files.
For example, let's assume a simple config file that looks like this:
spec1.config
<?xml version="1.0" encoding="utf-8"?>
<Settings>
<add key="hostname" value="QA_hostname" />
<add key="username" value="QA_username" />
</Settings>
A very simple, hashtable-like (key-value pair) structure.
An implemented parser/reader would then look something like this:
private Hashtable getSettings(string path)
{
Hashtable _ret = new Hashtable();
if (File.Exists(path))
{
StreamReader reader = new StreamReader
(
new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.Read)
);
XmlDocument doc = new XmlDocument();
string xmlIn = reader.ReadToEnd();
reader.Close();
doc.LoadXml(xmlIn);
foreach (XmlNode child in doc.ChildNodes)
if (child.Name.Equals("Settings"))
foreach (XmlNode node in child.ChildNodes)
if (node.Name.Equals("add"))
_ret.Add
(
node.Attributes["key"].Value,
node.Attributes["value"].Value
);
}
return (_ret);
}
Meanwhile, you'll still be able to use ConfigurationManager.AppSettings[]
to read from the original app.config
file.