Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This worked --<br> Code:</p> <pre><code>class Program { static void Main(string[] args) { NameValueCollection nvc = ConfigurationManager.GetSection("MyAppSettings") as NameValueCollection; for(int i=0; i&lt;nvc.Count; i++) { Console.WriteLine(nvc.AllKeys[i] + " " + nvc[i]); } Console.ReadLine(); } } class ParentSection : ConfigurationSection { //This may have some custom implementation } class MyAppSettingsSection : ParentSection { public static MyAppSettingsSection GetConfig() { return (MyAppSettingsSection)ConfigurationManager.GetSection("MyAppSettings"); } [ConfigurationProperty("", IsDefaultCollection = true)] public NameValueConfigurationCollection Settings { get { return (NameValueConfigurationCollection)base[""]; } } } </code></pre> <p>Configuration: </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt; &lt;configSections&gt; &lt;!-- &lt;section name="MyAppSettings" type="CustomAppSettings.MyAppSettingsSection, CustomAppSettings"/&gt; --&gt; &lt;section name="MyAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/&gt; &lt;/configSections&gt; &lt;MyAppSettings&gt; &lt;add key="1" value="one"/&gt; &lt;add key="2" value="two"/&gt; &lt;add key="3" value="three"/&gt; &lt;add key="4" value="four"/&gt; &lt;/MyAppSettings&gt; &lt;/configuration&gt; </code></pre> <p>My major concern was that my section needed to inherit from a custom section and I wanted to return a NameValueCollection when the ConfigurationManager.GetSection("MyAppSettings") is called.<br> I changed the type property to AppSettingsSection, even though it's nowhere in the picture and it worked. Now I need to figure out how that worked but for now the good thing is that I have a working sample :)</p> <p><strong>Update</strong>: Unfortunately, this wasn't the expected way of achieving what was intended because now the custom section is not coming into the picture at all so unfortunately this isn't the best way to do it. </p> <p><strong>Of course, if you just wanted to rename your appsettings section this would work like a charm.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload