.Config file storage is the most common scenario for configuration information, as it's integrated with the .NET framework and respects auto-detection of changes in ASP.NET applications even for externally stored configuration files.

You can create default .config files in the AppSettings section (if no section name is provided), or you can create a custom section or seperate .config file for your configuration data via the ConfigurationSection and FileName properties.

.Config files also can update the object directly from the configuration data rather than using .NET serialization, so if you only use .config file access, implementation can be done with this simple code:

public MyAppConfig() : base(null,"MyAppConfiguration") {} // properties to persist below

If no customization beyond SectionName (passed optionally in the constructor) is required, you are done.

Example Provider Configuration

If you prefer to customize the provider, you should implement a custom constructor that takes a provider as an optional input.

.Config File operation is set up through the ConfigurationFileConfigurationProvider<T> Class which handles configuration as follows:

public class ApplicationConfiguration : AppConfiguration { public ApplicationConfiguration() { Initialize(); } public ApplicationConfiguration(IConfigurationProvider provider) { Initialize(); if (provider == null) { provider = new ConfigationFileConfigurationProvider<ApplicationConfiguration>() { PropertiesToEncrypt = "MailServerPassword,ConnectionString", EncryptionKey = "secret", ConfigurationSection = "ApplicationConfiguration" }; } Provider = provider; Read(); } // property definitions below }

Default Instantiation:
You can now instantiate the class with:

App.Configuration = new ApplicationConfiguration(null);

Provider Instantiation:
You instantiate the ConfigurationFileConfigurationProvider<T> class with the generic parameter of the type of your Configuration class implementation:

this.Provider = new ConfigationFileConfigurationProvider<ApplicationConfiguration>()

The provider then supports the following properties:

  • ConfigurationSection
    Optional configuration section name. If not specified the default appSettings section is used. Recommend you always set this to isolate your application's configuration settings clearly.

  • ConfigurationFile
    Optional external file to store config data in.

  • PropertiesToEncrypt
    Optional comma delimited list of properties to encrypt.

  • EncryptionKey
    Optional encryption key for the encrypted properties. If not specified when properties are to be encrypted a default key is used. Recommend you always set PropertiesToEncrypt and Encryption key at the same time.

Writing to Config Files

Configuration information can be written to the .config files using the Configuration object's Write() method.

if (!App.Configuration.Write()) this.ErrorDisplay.ShowError("Failed to write config: " + App.Configuration.ErrorMessage);

This writes the config object's current property state into the configuration store.

Web Applications that want to write to web.config or external config files need WRITE access for the Web user account (NETWORK SERVICE/ASPNET commonly). Write operations fail silently - you need to check the result value from Write() to detect errors.

Note that AppConfiguration will also try to implicitly write any missing values to the .config file after load if the section doesn't exist or individual values are not found.

Write operations in Web applications will cause the Web application AppDomain to restart so you'll want to write sparingly. Under normal conditions