CONFIGURATION FILES

Configuration files let you store information for a program to use at run time in a standardized external file. You can change the values in the configuration file, and the program will use the new value the next time it starts. That enables you to change some of the application’s behavior without needing to recompile the executable program.

One way to use configuration files is through dynamic properties. Dynamic properties are automatically loaded from the configuration file at run time by Visual Basic.

Start by defining the settings you will bind to the dynamic properties. In Solution Explorer, double-click My Project and select the Settings tab to see the property page shown in Figure 28-1. Use this page to define the settings that you will load at run time.

FIGURE 28-1: Use this page to define application settings.

image

A setting’s scope can be Application or User. A setting with Application scope is shared by all of the program’s users. Settings with User scope are stored separately for each user so different users can use and modify their own values.

Next, add a control to a form and select it. In the Properties window, open the ApplicationSettings entry, click the PropertyBinding subitem, and click the ellipsis to the right to display a list of the control’s properties.

Select the property that you want to load dynamically and click the drop-down arrow on the right to see a list of defined settings that you might assign to the property. Figure 28-2 shows the Application Setting dialog box with this drop-down list displayed for a control’s Text property. From the list, select the setting that you want to assign to the property.

FIGURE 28-2: Use the drop-down list to assign a setting to the dynamic property.

image

Visual Studio adds the setting to the program’s configuration file. If you open Solution Explorer and double-click the app.config entry, you’ll see the new dynamic property.

The following text shows the configuration setting sections of an App.config file. The userSettings section defines the settings shown in Figure 28-1.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    ...
    <userSettings>
        <ConfigFile.My.MySettings>
            <setting name="ForeColorName" serializeAs="String">
                <value>Blue</value>
            </setting>
            <setting name="BackColorName" serializeAs="String">
                <value>Yellow</value>
            </setting>
            <setting name="FontName" serializeAs="String">
                <value>Comic Sans MS</value>
            </setting>
            <setting name="FontSize" serializeAs="String">
                <value>48</value>
            </setting>
            <setting name="Font" serializeAs="String">
                <value>Comic Sans MS, 48pt</value>
            </setting>
            <setting name="ResultText" serializeAs="String">
                <value>Result</value>
            </setting>
            <setting name="ForeColorValue" serializeAs="String">
                <value>Blue</value>
            </setting>
            <setting name="BackColorValue" serializeAs="String">
                <value>Yellow</value>
            </setting>
        </ConfigFile.My.MySettings>
    </userSettings>
</configuration>

When the application starts, Visual Basic loads the App.config file, reads the settings, and assigns their values to any properties bound to them.

So far, this is just a very roundabout way to set the control’s property values. The real benefit of this method comes later when you want to change a setting. If you look in the compiled application’s directory (normally the binDebug directory when you’re developing the program), you’ll find a file with the same name as the application but with a .config extension. If the application is called ConfigFile.exe, then this file is called ConfigFile.exe.config.

If you open this file with any text editor and change the value of a setting, the program uses the new value the next time it runs. For example, if you change the value of BackColorValue from Yellow to Orange, then the next time the program runs, any controls that use that color for their backgrounds will now be orange. Instead of recompiling the whole application, you only need to change this text file. If you have distributed the application to a large number of users, you only need to give them the revised configuration file and not a whole new executable.

When you make a new setting, Visual Basic automatically generates code that adds the setting to the My.Settings namespace, so the program can easily get the setting’s value. For example, the following code displays the values of the txtFontSize and txtFontName settings:

MessageBox.Show(My.Settings.FontSize & "pt " & My.Settings.FontName)

The My.Settings namespace provides several other properties and methods that make working with settings easy. The following table summarizes the most useful My.Settings properties and methods:

PROPERTY OR METHOD PURPOSE
Item A name-indexed collection of the values for the settings.
Properties A name-indexed collection of SettingsProperty objects that contain information about the settings, including their names and default values.
Reload Reloads the settings from the configuration file.
Save Saves any modified settings into the configuration file. The program can modify settings with user scope. Settings with application scope are read-only.

Example program ShowSettings uses the following code to display the settings listed in the My.Settings.Properties collection:

Imports System.Configuration
 
Public Class Form1
    Private Sub Form1_Load() Handles MyBase.Load
        For Each settings_property As SettingsProperty In My.Settings.Properties
            Dim new_item As New ListViewItem(settings_property.Name)
            new_item.SubItems.Add(settings_property.DefaultValue.ToString)
            lvSettings.Items.Add(new_item)
        Next settings_property
 
        lvSettings.Columns(0).Width = -2
        lvSettings.Columns(1).Width = -2
    End Sub
End Class

When a program closes, it automatically saves any changes to User scope settings. However, if the program crashes, it does not have a chance to save any changes. If you want to be sure changes are saved, call My.Settings.Save after the user changes settings.

Example program SaveSettings uses two settings: a User scope color named FormColor that determines the form’s background color and an Application scope color named ButtonColor that determines the background colors of the program’s two buttons. The program provides Form Color and Button Color buttons to let you change the two color settings. If you change the colors, close the program, and restart it, you’ll see that the User scope form color is saved, but the Application scope button color is not.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.70.1