Unable to use NameValueSectionHandler in config section

G

Guest

I'm doing what I thought was a very simple access of an app.config element in
a section, with no custom section handler, just NameValue. Here's the config
file:

<configuration>
<configSections>
<section name="MySection"
type="System.Configuration.NameValueSectionHandler" />
</configSections>

<MySection>
<add key="Company" value="MyCompany" />
</MySection>
</configurations>

And here's the code:

Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

NameValueCollection nvc = (NameValueCollection)
config.GetSection("MySection");

I get a compiler error:

Error 52 Cannot convert type 'System.Configuration.ConfigurationSection' to
'System.Collections.Specialized.NameValueCollection'

I can't find any examples on the web about this, only older samples that use
ConfigurationSettings, not the preferred ConfigurationManager. Anybody know
what simple thing I'm doing wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top