.NET Framework Reading strongly-typed settings from custom section of configuration file


Starting from a new Settings class and custom configuration section:

Settings tab of the Project Properties Designer

Add an application setting named ExampleTimeout, using the time System.Timespan, and set the value to 1 minute:

Settings tab while adding ExampleTimeout application setting

Save the Project Properties, which saves the Settings tab entries, as well as re-generates the custom Settings class and updates the project configuration file.

Use the setting from code (C#):


using System;
using System.Diagnostics;
using ConsoleApplication1.Properties;

namespace ConsoleApplication1
    class Program
        static void Main()
            TimeSpan exampleTimeout = Settings.Default.ExampleTimeout;


Under the covers

Look in the project configuration file to see how the application setting entry has been created:

app.config (Visual Studio updates this automatically)

<?xml version="1.0" encoding="utf-8"?>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="ConsoleApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <appSettings />
      <setting name="ExampleTimeout" serializeAs="String">

Notice that the appSettings section is not used. The applicationSettings section contains a custom namespace-qualified section that has a setting element for each entry. The type of the value is not stored in the configuration file; it is only known by the Settings class.

Look in the Settings class to see how it uses the ConfigurationManager class to read this custom section.

Settings.designer.cs (for C# projects)

    public global::System.TimeSpan ExampleTimeout {
        get {
            return ((global::System.TimeSpan)(this["ExampleTimeout"]));

Notice that a DefaultSettingValueAttribute was created to stored the value entered in the Settings tab of the Project Properties Designer. If the entry is missing from the configuration file, this default value is used instead.