Dutton

appSettings, applicationSettings and custom configSections in app.config

For some reason the different ways of handling application settings in C# always throws me, probably because I tend to write it once and forget about it until it comes up again many months later.

<applicationSettings>

If you create a default settings file in my Project, which creates an app.config and a Settings.settings under Properties, entries here can either be in User or Application scope and result in entries added to the app.config file under <applicationSettings> or <userSettings> sections appropriately, looking like this:

 
<userSettings>
<AppConfigTest.Properties.Settings>
<setting name="MyUserScopeSetting" serializeAs="String">
<value>foo1</value>
</setting>
</AppConfigTest.Properties.Settings>
</userSettings>
<applicationSettings>
<AppConfigTest.Properties.Settings>
<setting name="MyApplicationScopeSetting" serializeAs="String">
<value>foo2</value>
</setting>
</AppConfigTest.Properties.Settings>
</applicationSettings>

These can then be accessed from within your code using:

 
var setting1 = Properties.Settings.Default.MyApplicationScopeSetting;
var setting2 = Properties.Settings.Default.MyUserScopeSetting;

These will appear under your Intellisense in Visual Studio.

You can also write to these, just make sure you call Save() afterwards:

 
Properties.Settings.Default.MyApplicationScopeSetting = "new value";
Properties.Settings.Default.Save();

<appSettings>

You might also find app.config files containing elements added directly to an appSettings section like this:

 
<appSettings>
<add key="MySetting1" value="Value 1" />
<add key="MySetting2" value="Value 2" />
</appSettings>

These can be accessed using the ConfigurationManager:

 
var setting1 = ConfigurationManager.AppSettings["MySetting1"];
var setting2 = ConfigurationManager.AppSettings["MySetting2"];

This is accessed by key string, so will not appear under Intellisense, but will return null if the key doesn't exist.

Note that there is no type checking with appSettings; you cannot safely assume the type of the configuration item and someone could have changed your integer to a string in the config file for all you know and you'd have to deal with it.

Custom configSection

A third, and I believe now recommended since .Net 2.0 approach is to create a custom configSection within app.config. This needs to be represented in code, through a class which derives from ConfigurationSection:

    public class MyExampleConfigurationSection : ConfigurationSection
    {
        private static readonly ConfigurationProperty MyExampleString;

        private static readonly ConfigurationPropertyCollection ConfigurationProperties;

        static MyExampleConfigurationSection()
        {
            // Define your configuration properties here
            MyExampleString = new ConfigurationProperty("myExampleStringValue", typeof (string), null,
                                                         ConfigurationPropertyOptions.IsRequired);

            ConfigurationProperties = new ConfigurationPropertyCollection {MyExampleString};
        }

        [ConfigurationProperty("myExampleStringValue", IsRequired = true)]
        public string MyExampleStringValue
        {
            get { return (string)base[MyExampleString]; }
        }

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

Like the userSettings and applicationSettings sections, built-in configuration sections must be explicitly defined in a new sectionGroup under configSections, like so:

 
<configSections>
<section name="example" type="AppConfigTest.MyExampleConfigurationSection, AppConfigTest"/>

</configSections>

can be set like this:

 
<example myExampleStringValue="Foo"/>

and then queried in code like this:

             var myExampleConfigurationSection =
                ConfigurationManager.GetSection("example") as MyExampleConfigurationSection;
            if (myExampleConfigurationSection != null)
            {
                Console.WriteLine(myExampleConfigurationSection.MyExampleStringValue);
            }

Share this: