Share via


ConfigurationSection for NameValueSectionHandler

Question

Friday, September 16, 2011 6:02 AM

I have a custom section using NameValueSectionHandler, in a config file (called AddinApp.config).

Programatically, I want a get a value of key ImpersonationWcfServices.Password.

    var localdllPath = @"C:\Addins\AddinTest\MyApp.Addin.config";

    Configuration addInConfig = ConfigurationManager.OpenExeConfiguration(localdllPath);

    ConfigurationSection section = config.GetSection("ImpersonationSettings");

How can I get the value ??

    <configSections>

    <section name="ImpersonationSettings" type="System.Configuration.NameValueSectionHandler" />

    </configSections>

    

    

    <ImpersonationSettings>

   

    <add key="ImpersonationWcfServices.Password" value="xxx" />

    

    </ImpersonationSettings>

thanks in advanced

Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts

All replies (7)

Friday, September 16, 2011 9:06 AM

You have to cast the section to a NameValueCollection:

NameValueCollection section = config.GetSection("ImpersonationSettings") as NameValueCollection;
string impersonationWcfServicesPassword = section["ImpersonationWcfServices.Password"];

Paulo Morgado


Friday, September 16, 2011 9:22 AM

Not is possible

 

                    var section1 = config.GetSection("ImpersonationSettings") as System.Collections.Specialized.NameValueCollection;

//Error 

GetSection returns ConfigurationSection type, and NameValueCollection is not ConfigurationSection 

This it compiles...but how I get the value ??

ConfigurationSection section = config.GetSection("ImpersonationSettings");

 

 

Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts


Friday, September 16, 2011 9:42 AM | 1 vote

Sorry about that.

NameValueSectionHandler is a .NET 1.1 concept and it doesn't seem to be supported by the new model.

Try this:

NameValueCollection section = System.Configuration.ConfigurationManager.GetSection("ImpersonationSettings") as NameValueCollection;
string impersonationWcfServicesPassword = section["ImpersonationWcfServices.Password"];

You can only use it from inside the application and cannot explicitly open the configuration file.

Paulo Morgado


Friday, September 16, 2011 10:04 AM

I cannot, I use Addin VS and I need explicitly open the configuration file.Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts


Saturday, September 17, 2011 5:06 PM

You could try using a AppSettingsSection instead.

Paulo Morgado


Wednesday, September 21, 2011 9:23 AM

I need custom section. Thanks anyway.Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts


Wednesday, September 21, 2011 11:08 AM

I'm not telling you to use <appSettings>. I'm telling you to try System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0 instead of System.Configuration.NameValueSectionHandler, System, Version=2.0.0.0.

Paulo Morgado