Example code :
EDITED : clarified example. Sorry for any confusion.
using System.Collections.Specialized;
using System.Configuration;
...
// get collection 1
NameValueCollection c1 = ConfigurationManager.AppSettings;
// get collection 2
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = "c:\\SomeConfigFile.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
KeyValueConfigurationCollection c2 = config.AppSettings.Settings;
// do something with collections
DoSomethingWithCollection(c1);
DoSomethingWithCollection(c2);
...
private void DoSomethingWithCollection(KeyValueConfigurationCollection c)
{
foreach(KeyValueConfigurationElement el in c)
{
string key = el.Key;
string val = el.Value;
// do something with key and value
}
}
private void DoSomethingWithCollection(NameValueCollection c)
{
for(int i=0; i < c.Count; i++)
{
string key = c.GetKey(i);
string val = c.Get(i);
// do something with key and value
}
}
There are currently two versions of DoSomethingWithCollection to take either a NameValueCollection or KeyValueConfigurationCollection.
Is there a cleaner way of doing this, so that there is just one version of DoSomethingWithCollection ?
Thanks.