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.
.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:
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