Using the New Application Settings System


The new application settings system that is an integral part of all.NET Framework 2.0 applications dramatically increases the power of configuration files.

In previous versions of the .NET Framework, application configuration files were used to provide read-only information that could be used to feed data into the application at startup. The new application settings framework provides that same functionality, but also allows the developer to

  • Visually create settings using a designer

  • Programmatically read settings at User or Application scope

  • Save settings to the local file system at User or Application scope

  • Bind control properties (read and write) to User or Application scope configuration settings

You have already seen a little bit of the first item when you created a web reference. Right-click your Windows Forms project, select Properties, and then click the Settings tab. You will see the settings designer shown in Figure 38.1.

Figure 38.1. The settings designer.


This designer looks very much like the resource editor. To add a new setting, simply provide a name, a data type, and a scope (User or Application). In previous versions of the .NET Framework, the developer had to resort to writing tedious isolated storage code to store and retrieve user-scoped preferences and options.

When your application starts, all applicable settings are automatically loaded. This means that all application-scoped settings are loaded and all user-scoped settings for the current user are loaded. Controls that are bound to configuration file properties will load those properties from the configuration system whenever those values are needed.

The first time you use the Settings Designer class within Visual Studio, a Settings class is created for you. This class provides encapsulation around the settings in the designer, as well as strongly typed member access. Using this class, you can read and write settings and save those settings, as shown in the following code:

MessageBox.Show("Your zip code is "+     Properties.Settings.Default.ZipCode); Properties.Settings.Default.ZipCode = txtZipCode.Text; Properties.Settings.Default.Save(); 


The Default property provides access to an instance of the class created by the designer. If you like, you can write your own C# code and extend this class by modifying the Settings.cs class that has been added to the Visual Studio solution.

One of the most powerful things you can do with the new application settings system is to bind control properties to configuration properties. To do this, create a new string property called Greeting and make sure it is scoped at the User level. Next, drag a TextBox onto the form and call it txtGreeting. In the txtGreeting properties panel, expand the ApplicationSettings group at the top and click the ellipsis button next to PropertyBinding. You will see a configuration setting property map dialog like the one in Figure 38.2.

Figure 38.2. Configuration setting property map dialog.


This binding only handles reading the data from the configuration system when the application starts up. If the data changes, you have to call Properties.Settings.Default.Save() in your code to persist the change. The really powerful aspect of this new system is that the scoping is dealt with automatically. Without your having to write any additional code, your application can automatically differentiate between preferences set by any of the application's users as well as maintain application-wide settings.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

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