Getting the values from a config file that uses a section defined by System.Configuration.NameValueSectionHandler is easy when you're using the current config file for the application.
Example Config file.
<configuration>
<configSections>
<section name="MyParams" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<MyParams>
<add key="FirstParam" value="One"/>
<add key="SecondParam" value="Two"/>
</MyParams>
</configuration>
Example Code that easily reads it.
NameValueCollection myParamsCollection =
ConfigurationManager.GetSection("MyParams") as NameValueCollection;
This is the code that doesn't work.
NameValueCollection collection =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
.GetSection("MyParams") as NameValueCollection;
That fails with the following error on compile.
Cannot convert type 'System.Configuration.ConfigurationSection' to 'System.Collections.Specialized.NameValueCollection' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion.
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) returns a System.Configuration.Configuration, and Configuration.GetSection returns ConfigurationSection.
ConfigurationManager.GetSection returns object.
So, how do I get back my NameValueCollection when I have to use OpenExeConfiguration?