advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   TIP BANK
Browse DevX
Download the code for this article
Were you aware that you could alter configuration files with the System.Xml classes? Do you think it's a good idea? Why or why not? If so, when do you think it's appropriate? Did this article meet your needs? Let us know in the vb.dotnet.technical discussion group.
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
 

How to Make Your .NET Windows Forms Configuration Files Dynamic

Although Windows Forms configuration files are read-only within the framework, they become dynamic XML databases when you create this AppSettingsManager class. 


advertisement
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 advance—you 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 files—the sample code does not work with ASP.NET web.config files.

  Next Page: Configuration File Name and Location


      Register for free to read this and other premium DevX content!

      Already Registered? Click here to Login

  • About You

  • Contact Information

  • - -      Ext: (optional)
  • Company Information




  • Create Password

Page 1: IntroductionPage 4: Creating, Altering, and Deleting Settings
Page 2: Configuration File Name and LocationPage 5: Using the AppSettingsManager
Page 3: Building the AppSettingsManager Class 
advertisement