he .NET framework supports configuration
files to supply settings and static data to both ASP.NET and Windows Forms applications. Configuration files are XML-formatted files loaded at startup time and cached in memory so that you have fast access to data. Essentially, configuration files are intended to replace both the Win16 INI file format and application-specific registry entries for managed code.
As delivered, the .NET framework supports a number of different configuration sections and tags within configuration files, including an <appSettings>
tag, which can contain a list of child elements that hold data specific to your application. There's one problem though; the native .NET classes can read
the configuration data, but can't alter
it. Why would you want to alter the data? Here are two common examples:
- You want your application to "remember" its window size and position between sessions.
- You want to create user-configurable options, such as toolbar buttons, color themes, or favorites lists, and store them with the application.
You can't know these values in advanceyou need to store and change them at runtime. But if you can't use the framework classes to write to the files, you need to create some other way to alter them. Fortunately, as the files are well-formed XML, you're perfectly free to create new classes or to use classes in the System.Xml namespace to alter the contents of your configuration files.
There are some significant differences between the way Windows Forms and ASP.NET applications interact with configuration files. Although both read the files at startup and cache the contents, if you change a Windows Forms configuration file, the changes don't take effect until the next time you start the application. In contrast, when you change an ASP.NET applications configuration file, the framework recognizes the change immediately.
|Author's Note: This article applies only to Windows Forms configuration filesthe sample code does not work with ASP.NET web.config files.