2005-07-27
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 2 of 3 )
Almost every application program has to save some information between sessions. No, I am not talking about a program's data files, although that's essential too. Rather, in this article, I am referring to application settings that affect the way the application runs.
The term application settings really encompasses two different kinds of settings:
- Application settings, per se, are those that ship with the application, affect all users, and are generally meant to be changed only by an administrator.
- User settings are specific to each user and include information such as the most recent file list, program window size, and toolbar customization. User settings need to be saved before the application quits and retrieved when the application starts.
Some programmers are not clear on the distinction between these types of settings. It's important that they be treated and stored separately. Let's see why.
Application Settings
The .Net Framework provides excellent support for application settings. By selecting Project, Add New Item and then selecting Application Configuration File, you create a file named App.Config. This is an XML file where you place your settings using the following format:
<?xml version="1.0" encoding="utf-8" ?>
<configuration> <appSettings> <add key="Database Server" value="NewYorkSQLServer" /> <add key="Help URL" value="www.somewhere.com/help.htm" /> </appSettings> </configuration>
The first, second, and last lines of this file are created for you — then you add the required information as shown here using XML tags. When the application is compiled, this file is automatically renamed xxxx.exe.config (where xxxx is the application name) and placed in the output folder. When the application is installed, this file goes along, and it is automatically loaded when the application runs.
Your code can access the settings in the file using the classes in the System.Configuration namespace. For example, to retrieve the "Help URL" value you would write:
Dim HelpURL As String
HelpURL = ConfigurationSettings.AppSettings("Help URL")
These tools for creating and saving application settings are well designed and easy to use. So many programmers think, "Aha! I can use this to save user settings also." But then, after a fruitless search through the .Net documentation, they discover that the System.Configuration namespace does not provide any way to change or add to the App.Config file. There's a good reason for this. App.Config is not, I repeat not, intended for saving user settings.
Why not? I thought you would never ask.
![]() |
|


