Configuration Files


Configuration files let you store information for a program to use at runtime 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 runtime 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 27-5. Use this page to define the settings that you will load at runtime.

image from book
Figure 27-5: Use this page to define application settings.

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 27-6 shows the Application Setting dialog box with this drop-down list displayed. From the list, select the setting that you want to assign to the property.

image from book
Figure 27-6: Use the drop-down list to assign a setting to the dynamic property.

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 is empty. The applicationSettings section defines the settings shown in Figure 27-5.

  <?xml version="1.0" encoding="utf-8" ?> <configuration>     ...     <userSettings>         <ConfigFile.Settings />     </userSettings>     <applicationSettings>         <ConfigFile.Settings>             <setting name="txtBackColor" serializeAs="String">                 <value>Yellow</value>             </setting>             <setting name="txtForeColor" serializeAs="String">                 <value>Blue</value>             </setting>             <setting name="txtFontName" serializeAs="String">                 <value>Comic Sans MS</value>             </setting>             <setting name="txtFontSize" serializeAs="String">                 <value>50</value>             </setting>             <setting name="clrForeColor" serializeAs="String">                 <value>Blue</value>             </setting>             <setting name="clrBackColor" serializeAs="String">                 <value>Yellow</value>             </setting>             <setting name="fntFont" serializeAs="String">                 <value>Comic Sans MS, 48pt</value>             </setting>         </ConfigFile.Settings>     </applicationSettings> </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 value. The real benefit of this method comes later when you want to change this setting. If you look in the compiled application’s directory (normally the bin\Debug 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 the TheDynamicValue setting, the program uses the new value the next time it runs. Instead of recompiling the whole application, you only need to change this simple text file. If you have distributed the application to a large number of users, you need only 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. That makes the values easy for a program to read. The following code displays the values of the TheUserSetting and TheAppSetting settings:

  MessageBox.Show(My.Settings.txtFontSize & "pt " & My.Settings.txtFontName) 

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.

Open table as spreadsheet

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.

The following example uses the My.Settings.Properties collection to list all of the application’s settings and their values:

  Imports System.Configuration Public Class Form1     Private Sub Form1_Load(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles MyBase.Load         Dim txt As String = ""         For Each settings_property As SettingsProperty In My.Settings.Properties             txt &= settings_property.Name & " = " & _                 settings_property.DefaultValue.ToString & vbCrLf         Next settings_property         txtSettings.Text = txt         txtSettings.Select(0, 0)     End Sub End Class 

Tip 

The new security features in Windows Vista make saving configuration information more difficult. If an application is installed in a protected part of the file system, a normal user won’t be able to save configuration settings there. If the program saves settings on a per-user basis in the user’s own directories, the changes won’t affect other users. Similarly, the SaveSettings command saves values into the Registry separately for each user, so changes made with SaveSettings don’t affect other users. One solution is to save user-specific settings in the user’s directories, and save application-wide settings in a file stored in a common directory that all users can access.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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