Application Settings


As some applications get more sophisticated, users expect more from all their applications. For example, some applications let users set fonts and colors and all kinds of crazy things ( guaranteeing that you'll never be able to successfully test every combination). In addition to the other fancy user interface (UI) features that it provides, WinForms supports the idea of reading a control's properties from an application's configuration file, also called a .config file .

.NET allows every application to have a .config file associated with it. For example, when .NET loads MySecondApp.exe, it will look for a corresponding MySecondApp.exe.config file in the same folder as the application.

A .config file can have standard settings for things such as security, remoting, loading, and versioning. In addition to standard .NET settings, a configuration file can have custom settings strictly for the application's use. For example, the following is a .config file containing settings specifying a custom key-value pair:

 <configuration>   <appSettings>  <add key="MainFormOpacity" value=".5" />  </appSettings> </configuration> 

Notice that the .config file is in XML format. This means that savvy users can open the .config file in Notepad to change the setting. For this setting to be used to set the main form's opacity property, the setting needs to be read:

 using System.Configuration; public class MainForm : System.Windows.Forms.Form {   public MainForm() {     InitializeComponent();     // After InitializeComponent call  AppSettingsReader appSettings = new AppSettingsReader();   object o = appSettings.GetValue("MainFormOpacity", typeof(double));   this.Opacity = (double)o;  }   ... } 

Instead of opening the .config file directly, this code uses the AppSettingsReader function from the System.Configuration class. This class provides access to the key-value pairs in the <appSettings> section of the .config file associated with the application. We use the reader to get the value of the MainFormOpacity key and use that to set the property on the form.

If this is the kind of thing you'd like to provide in your application's forms for a wide variety of properties, it will be a chore to manually pull each value from the AppSettingsReader. For this reason, each control on each form, as well as the form itself, has a set of dynamic properties available in the Property Browser. The dynamic properties are those automatically read from the application's .config file. For example, to tell the Designer to generate code to pull in the opacity setting for the main form, you click the Advanced property's " " button under the Dynamic Properties for the form, bringing up the list of potential properties to be made dynamic, as shown in Figure 1.14.

Figure 1.14. Dynamic Properties for MainForm

Any property checked in this dialog will be read from the .config file. Notice the key mapping provided by the dialog after we've chosen to make Opacity dynamic. This mapping is the key that will be used in the .config file.

After you've chosen a dynamic property, three things happen. First, a file called app.config is added to your project. This file will be automatically copied into the output directory of your project as it is built and will be renamed to match the name of your application, saving you from having to manually keep .config files up-to-date in Release and Debug directories.

The second thing that happens is that the contents of app.config will be populated with whatever value is set in the Property Browser for that property. For example, the following app.config file will be generated for MainForm.Opacity ( assuming a default opacity value of 1):

 <?xml version="1.0" encoding="Windows-1252"?> <configuration>   <appSettings>     <!--   User application and configured property settings go here.-->     <!--   Example: <add key="settingName" value="settingValue"/> -->  <add key="MainForm.Opacity" value="1" />  </appSettings> </configuration> 

The final thing that happens for each dynamic property is that the InitializeComponent function is augmented with code to pull in the properties at run time, saving you the need to write that code yourself:

 public MainForm() {     InitializeComponent(); } private void InitializeComponent() {  System.Configuration.AppSettingsReader configurationAppSettings =   new System.Configuration.AppSettingsReader();  ...  this.Opacity =   ((System.Double)(configurationAppSettings.GetValue(   "MainForm.Opacity", typeof(System.Double))));  ... } 

As useful as the .config file is, it's not for everything. In fact, its usefulness is limited to read-only machinewide application settings because the AppSettingsReader has no matching AppSettingsWriter. Instead, machinewide or per-user application settings that can be changed, such as the position of the main form between sessions, should be kept either in a file in an operating system “provided special folder or, even better, in a .NET-specific place called isolated storage . Both of these are covered in detail, along with a discussion of application lifetime and environment, in Chapter 11: Applications and Settings.



Windows Forms Programming in C#
Windows Forms Programming in C#
ISBN: 0321116208
EAN: 2147483647
Year: 2003
Pages: 136
Authors: Chris Sells

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net