Search code examples
c#.netconfigurationmanager

Cannot get custom configuration section to work in app.config


I followed a tutorial on Stackoverflow to add a custom configuration section to my exe's config file, however on calling it, it is returning null. It's not even getting into the static constructors so something is clearly wrong, but I can't see what.

Here is my config file and the section I wish to find.

<configuration>
  <appSettings>
  </appSettings>
      <configSections>
           <section name="PresetFilters" type="ImageTool.PresetFiltersConfiguration, ImageTool" />
      </configSections>
  <PresetFilters>
    <add key="Default,-20,0,0,0,0" />
    <add key="No Change,0,0,0,0,0" />
    <add key="Dark Photo,10,10,0,0,-10" />
  </PresetFilters>
</configuration>

I call it like this:

 PresetFiltersConfiguration pf = (PresetFiltersConfiguration)ConfigurationManager.GetSection("PresetFilters");

and it returns null and doesn't even enter my class or class statics. Here's the code. Any help would be appreciated. Thanks.

public class PresetFiltersConfiguration : ConfigurationSection
{
    private static ConfigurationPropertyCollection properties;
    private static ConfigurationProperty propPresets;

    static PresetFiltersConfiguration()
    {
        propPresets = new ConfigurationProperty(null, typeof(PresetFiltersElementCollection),
                                                      null,
                                                      ConfigurationPropertyOptions.IsDefaultCollection);
        properties = new ConfigurationPropertyCollection { propPresets };
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return properties;
        }
    }

    public PresetFiltersElementCollection PresetFilter
    {
        get
        {
            return this[propPresets] as PresetFiltersElementCollection;
        }
    }
}

public class PresetFiltersElementCollection : ConfigurationElementCollection
{
    public PresetFiltersElementCollection()
    {
        properties = new ConfigurationPropertyCollection();
    }

    private static ConfigurationPropertyCollection properties;

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return properties;
        }
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.BasicMap;
        }
    }

    protected override string ElementName
    {
        get
        {
            return "add";
        }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new PresetFiltersElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        var elm = element as PresetFiltersElement;
        if (elm == null) throw new ArgumentNullException();
        return elm.KeyName;
    }
}

public class PresetFiltersElement : ConfigurationElement
{
    private static ConfigurationPropertyCollection properties;
    private static ConfigurationProperty propKey;

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return properties;
        }
    }

    public PresetFiltersElement()
    {
        propKey = new ConfigurationProperty("key", typeof(string),
                                                      null,
                                                      ConfigurationPropertyOptions.IsKey);
        properties = new ConfigurationPropertyCollection { propKey };
    }

    public PresetFiltersElement(string keyName)
        : this()
    {
        KeyName = keyName;
    }

    public string KeyName
    {
        get
        {
            return this[propKey] as string;
        }
        set
        {
            this[propKey] = value;
        }
    }
}

Solution

  • You need something like this in your app.config, otherwise the application won't know how to process your new section.

      <configSections>
        <sectionGroup name="pageAppearanceGroup">
          <section 
            name="PresetFilters" 
            type="PresetFiltersConfiguration" 
            allowLocation="true" 
            allowDefinition="Everywhere"
          />
        </sectionGroup>
      </configSections>