Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Use Windows Forms Application Settings to Personalize Your Applications : Page 2

Application Settings is a new feature in Windows Forms 2.0 that allows you to maintain both user- and application-specific data (known as settings). Find out how to use it to improve your application's usability with personalization.


advertisement
What Just Happened?
Let's examine the project a little closer to understand how the settings work. Figure 4 shows the three files in Solution Explorer that are used to maintain your application settings.

Figure 4. Maintain Your Settings: These three files can be found in Solution Explorer.

The Settings.settings file refers to the Settings page that you have been using to add the settings.



The app.config file is an XML File containing the details of your settings. See Listing 1 for the content of app.config.

Note the code in bold showing the settings that you have added earlier and their default values (they are all defined under the app.config file will be renamed <assembly_name>.exe.config. In this example, the filename is AppSettingsInCS.exe.config. You can verify this in the bin\Debug (or bin\Release) folder of your project.

For the Settings.Designer.cs file, it contains the data types of the various settings that you have defined. The definition for the FormBackgroundColor setting is shown below:

[global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] public global::System.Drawing.Color FormBackgroundColor { get { return ((global::System.Drawing.Color) (this["FormBackgroundColor"])); } set { this["FormBackgroundColor"] = value; } }

During runtime, each individual user (of your computer) will maintain his/her own copy of the app.config file, which will be named user.config. This file can be found within the following directory:

C:\Documents and Settings\\Local Settings\Application Data\<Project Name>

In my case, it is:

C:\Documents and Settings\Wei-Meng Lee\Local Settings\Application Data\AppSettingsInCS

Within this folder, you will find another folder, named something like:

AppSettingsInCS.vshost.ex_Url_dcjtbf3vhoeael5lvzjkefwbzccd3hbz

And within this folder, you will find a folder based on the version number of the application (in my case it is 1.0.0.0).

Here, you will find the user.config file:

<configuration> <userSettings> <AppSettingsInCS.Properties.Settings> <setting name="FormBackgroundColor" serializeAs="String"> <value>255, 255, 128</value> </setting> <setting name="FormSize" serializeAs="String"> <value>186, 241</value> </setting> <setting name="FormPosition" serializeAs="String"> <value>1817, 330</value> </setting> </AppSettingsInCS.Properties.Settings> </userSettings> </configuration>

As you can see, this file contains the values stored during runtime. As you change the color of the form, resize the form, or move the form, the updated values will be saved into this file. If you delete the user.config file, the application will revert to the default values found in AppSettingsInCS.exe.config.

Figure 5. The Settings Page: Adding the two new application-scope settings.

Application Scope
So far, you’ve been defining user-scope settings. Another scope you can use is Application scope. Unlike user-scope settings, which are used to store values specific to each individual users, application-scope settings are used for all users. A good example of how to use application-scope settings is to use them to store database connection strings.

To start, add two new settings to the grid in the Settings page (see Figure 5). The first connstr, is of type "(Connection string)" and it has Application scope (notice that the scope for a connection string can only be Application, not User). For its value, you can either type in the connection string, or click on the "…" button to bring up the dialog window to help you create the connection string. The second application setting—About, is of type string and also of Application scope.

Examine the app.config file (Listing 2), you will see that they are now added.

Interestingly, a connection string-type of setting is stored in its own section of the file (under the <connectionStrings> element). Other application-scope settings are stored under the <applicationSettings> element.

The definitions for the two application-scope settings are shown in Listing 3.

To see how these two application settings are used, add a second Button control to the form (see Figure 6).

Figure 6. Using the Application Settings: Add a second button to the form.

Double-click on the Button control to reveal its Click event handler and code (Listing 4).

As usual, access the connstr and About settings using the Properties.Settings.Default class.

For the connstr setting, you can also access it using the ConnectionStrings collection from the ConfigurationManager class (you need to add a reference to System.Configuration.dll):

string connStr = ConfigurationManager.ConnectionStrings[ "AppSettingsInCS.Properties.Settings.connstr"].ToString();

One important point to note is that application-scope settings are read-only during runtime, and can only be modified by an administrator once the application has been deployed. User-scoped settings, on the other hand, can be modified during runtime.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date