Custom Config section in App.config C#
Asked Answered
W

3

17


I'm a quite beginner with config sections in c#
I want to create a custom section in config file. What I've tried after googling is as the follows
Config file:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="MyCustomSections">
      <section name="CustomSection" type="CustomSectionTest.CustomSection,CustomSection"/>
    </sectionGroup>
  </configSections>

  <MyCustomSections>
    <CustomSection key="Default"/>
  </MyCustomSections>
</configuration>


CustomSection.cs

    namespace CustomSectionTest
{
    public class CustomSection : ConfigurationSection
    {
        [ConfigurationProperty("key", DefaultValue="Default", IsRequired = true)]
        [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
        public String Key
        {
            get { return this["key"].ToString(); }
            set { this["key"] = value; }
        }
    }
}


When I use this code to retrieve Section I get an error saying configuration error.

var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection("CustomSection");


What am I missing?
Thanks.

Edit
What I need ultimately is

    <CustomConfigSettings>
    <Setting id="1">
        <add key="Name" value="N"/>
        <add key="Type" value="D"/>
    </Setting>
    <Setting id="2">
        <add key="Name" value="O"/>
        <add key="Type" value="E"/>
    </Setting>
    <Setting id="3">
        <add key="Name" value="P"/>
        <add key="Type" value="F"/>
    </Setting>
</CustomConfigSettings>
Whiteeye answered 13/10, 2012 at 22:10 Comment(1)
Please review the following questions: #3935831 https://mcmap.net/q/743600/-custom-configurationsection #5238Polybius
S
40

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="customAppSettingsGroup">
      <section name="customAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </sectionGroup>
  </configSections>
  <customAppSettingsGroup>
    <customAppSettings>
      <add key="KeyOne" value="ValueOne"/>
      <add key="KeyTwo" value="ValueTwo"/>
    </customAppSettings>
  </customAppSettingsGroup>
</configuration>

Usage:

NameValueCollection settings =  
   ConfigurationManager.GetSection("customAppSettingsGroup/customAppSettings")
   as System.Collections.Specialized.NameValueCollection;

if (settings != null)
{
 foreach (string key in settings.AllKeys)
 {
  Response.Write(key + ": " + settings[key] + "<br />");
 }
}
Schoolfellow answered 13/10, 2012 at 22:46 Comment(2)
For anyone wanting to get this to work quickly. 3 things : 1. YOU MUST ADD a Reference to System.Configuration in your references, 2. using System.Configuration; 3. using System.Collections.Specialized;Fricassee
You can also omit the group (customAppSettingsGroup) if you want.Avarice
T
1

Try using:

var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection("MyCustomSections/CustomSection");

You need both the name of the section group and the custom section.

Tedmann answered 13/10, 2012 at 22:13 Comment(1)
Thanks for your response but this didn't work for me somehow. please look at edited part of the question.Whiteeye
H
-2

Highlight ConfigurationSection press F1, You will see that the implementation on the MSDN website overrides a property called "Properties" which returns a "ConfigurationPropertyCollection", as your properties have a matching attribute of that type you should be able to populate this collection with your properties if not wrap them in the same way the MS guys have.

Hyperphysical answered 7/10, 2013 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.