Settings

As noted earlier in the discussion of the Solution Explorer, the default project template does not create any application settings; accordingly, an app.config file is neither needed nor created. app.config files are XML files that define any custom application settings that a developer wants to be able to change without needing to recompile the application. Because these settings live in an XML file, they can be modified in between or even during application execution.

One original goal of .NET was to reduce the version conflict that can occur when a component has registered with global settings. A conflict would occur if two different applications were attempting to reference two different versions of that component. Because the settings were global and stored in the central system registry, only one could be registered correctly. Since the different applications each wanted its specific version of the component and related settings, one of the applications worked while the other application broke.

.NET provided the capability to place version-specific project references in a local directory with the application, enabling two different applications to reference the appropriate version of that component. However, the second part of the problem was the central application settings. The app.config file provides the same capability, but its goal is to allow for local storage of application settings. Under .NET 1.x, support for application settings was still minimal, as most developers were still looking to the central system registry for this purpose. At the same time, the developer tools associated with settings were also minimal.

Fortunately, under .NET 2.0 this changed dramatically. Visual Studio 2010 provides significant support for application settings, including the Settings tab, shown in Figure 1-12. This tab enables Visual Basic developers to identify application settings and automatically create these settings within the app.config file.

FiGuRE 1-12

Figure 1-12 illustrates several elements related to the application settings capabilities of Visual Basic. The first setting is of type String. Under .NET 1.x, all application settings were seen as strings, and this was considered a weakness. Accordingly, the second setting, LastLocation, exposes the Type drop-down, illustrating that under Visual Studio 2010 you can create a setting that has a well-defined type.

FiGuRE 1-12

Figure 1-12 illustrates several elements related to the application settings capabilities of Visual Basic. The first setting is of type String. Under .NET 1.x, all application settings were seen as strings, and this was considered a weakness. Accordingly, the second setting, LastLocation, exposes the Type drop-down, illustrating that under Visual Studio 2010 you can create a setting that has a well-defined type.

However, strongly typed settings are not the most significant set of changes related to application settings. The very next column defines the scope of a setting. There are two possible options: application wide or user specific. The settings defined with application scope are available to all users of the application. As shown in Figure 1-12, this example creates a sample connection string to store for the application.

The alternative is a user-specific setting. Such settings have a default value; in this case, the last location defaults to 0,0. However, once a user has read that default setting, the application generally updates and saves the user-specific value for that setting. As indicated by the LastLocation setting, each user of the application might close it after having moved it to a new location on the screen; and the goal of such a setting would be to reopen the application where it was last located. Thus, the application would update this setting value, and Visual Basic makes it easy to do this, as shown in the following code:

My.Settings.LastLocation = Me.Location My.Settings.Save()

That's right — Visual Basic requires only two lines of code that leverage the My namespace in order for you to update a user's application setting and save the new value. Meanwhile, let's take a look at what is occurring within the newly generated app.config file. The following XML settings demonstrate how the app.config file defines the setting values that you manipulate from within Visual Studio:

<?xml version="1.0" encoding="utf-8" ?> <configuration>

<configSections>

<sectionGroup name="userSettings" type="System.Configuration. UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

<section name="ProVB_VS2 010.My.MySettings" type="System. Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5 6193 4e08 9" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> </sectionGroup>

<sectionGroup name="applicationSettings" type="System.Configuration. ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5 6193 4e08 9" >

<section name="ProVB_VS2010.My.MySettings" type="System.Configuration. ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5 6193 4e08 9" requirePermission="false" /> </sectionGroup> </configSections> <system.diagnostics> <sources>

<!-- This section defines the logging configuration for My.Application.Log --> <source name="DefaultSource" switchName="DefaultSwitch"> <listeners>

<add name="FileLog"/>

<!-- Uncomment the below section to write to the Application Event Log --> <!--<add name="EventLog"/>--> </listeners> </source> </sources> <switches>

<add name="DefaultSwitch" value="Information" /> </switches> <sharedListeners>

<add name="FileLog"

type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft. VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"

initializeData="FileLogWriter"/> <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->

<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/>--> </sharedListeners> </system.diagnostics> <userSettings>

<ProVB_VS2010.My.MySettings>

<setting name="LastLocation" serializeAs="String">

<value>0, 0</value> </setting> </ProVB_VS2010.My.MySettings> </userSettings> <applicationSettings>

<ProVB_VS2010.My.MySettings>

<setting name="ConnectionString" serializeAs="String">

<value>server=(local);Database=adventureworks</value> </setting> </ProVB_VS2010.My.MySettings> </applicationSettings> </configuration>

Code snippet from app.config

As shown here, Visual Studio automatically generated all the XML needed to define these settings and save the default values. Note that individual user settings are not saved back into the config file, but rather to a user-specific working directory. In fact, it is possible not only to update application settings with Visual Basic, but also to arrange to encrypt those settings, although this behavior is outside the scope of what you can do from Visual Studio.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment