Operating with external *.config files


Operating with external *.config files in your application, ready to DataBinding.

Few time ago guys from my team asked me to develop little WPF app which will be a configurator for their big project parts. Every project module is a plain C# executable or dll with its own *.config file with <appSettings> section inside.

So I've opened VS, created new WPF app and started exploring configuration API of .NET

I wanted to bind settings directly to controls, so I've started writing some wrapper class, inheriting from ConfigurationSection class.

But soon I found a problem – AppSettingsSection class behave not exactly as ConfigurationSection behaves. So I understand that I should somehow modify wrapper class from popular example to make it work with AppSettingsSection class.

Here what I've got.

We have wrapper class Client_exe_AppConfigSectionHandler which will care of some Client.exe.config file with ValidBrushColor property inside <appSettings> section.

Solution was to store AppSettingsSection as a member and ask for particular settings via its public Settings property.

public sealed class Client_exe_AppConfigSectionHandler : ConfigurationSection
     {
           public Client_exe_AppConfigSectionHandler()
           {

           }

           public AppSettingsSection StoredSection;

           [ConfigurationProperty( "ValidBrushColor",
                DefaultValue = "107814",
                IsRequired = true,
IsKey = true )]
           public string validBrushColor
           {
                get
                {
                     return StoredSection.Settings[ "ValidBrushColor" ].Value;
                }
                set
                {
                     StoredSection.Settings[ "ValidBrushColor" ].Value = value;
               
}
           }
     }


Now you are ready to use Data Binding to bind properties to your WPF controls.