Introduction
. NET Framework 2.0 allows you to create and access applicationsProgramThe value of session persistence. These values are called settings. Setting can indicate user preferences or valuable information required by applications. For example, you can create a series of settings to store the user preferences of the application color scheme. You can also store the connection string of the database used by the specified application. You can setCodeImportant external applications, and you can create configuration files that store user preferences separately.
Use Visual Basic 2005MyThe namespace provides an obvious access setting mechanism, but there is no similar namespace in Visual C #2005, so it is a little difficult to set access. Even so, the C # user can still accessPropertiesNamespace to use settings. When reading this article, you will learn about the differences between application settings and user settings, how to create new settings at design time, and how to access the settings at runtime, and how to merge multiple sets into the application.
Application and user settings
The setting has four attributes:
Name: The Set "name" attribute refers to the name used to access the set value during runtime.
Type: "type" indicates the. NET Framework type. The setting can be of any type. For example, the setting that stores the user's color preferences will be of the system. color type.
Scope: the scope attribute indicates how to access the settings at runtime. The scope attribute has two possible values: Application and user ). This section will discuss these values more.
Value: the "value" attribute indicates the value returned during access settings. The value is the type indicated by the "type" attribute.
Most of these attributes are quite easy to understand. The concepts of "name", "type", and "value" should be well known to most programmers. The scope attribute must be described in a bit. The setting has two possible scopes: Application Scope and user scope. Setting with Application Scope indicates the setting that is used by the application regardless of the user preferences. Setting with user scope is usually not very important for the actual application, they are likely to be associated with preferences or other non-key values.
The important difference between application scope setting and user scope setting is that the user scope setting is read/write at runtime, and the value can be changed and saved in code. The application scope is set to read-only at runtime. Although it can be read, it cannot be written. Setting with application scope can only be changed at design time or by manually modifying the setting file.
Create new settings during design
You can use the setting designer to create new settings at design time. The setting designer uses a grid interface that everyone is familiar with. It allows you to create new settings and specify the properties of these settings. You must specify "name", "type", "Scope", and "value" for each new setting ). After the settings are created, you can evaluate them in the code using the mechanism described later in this article.
Steps for creating a new setting during design
In Solution Explorer, expand the properties node of the project.
In Solution Explorer, double-click the. settings file in which you want to add the new settings. The default name of this file is settings. settings.
In the setup designer, set "name", "type", "Scope", and "value" for the settings ). Each row represents a single setting. Figure 1 shows an example of the designer.
Figure
1. Visual Studio 2005
Set designer in Change existing settings during design
You can also use the setup designer to change the pre-configured values during design as described in the following steps:
Steps to change existing settings during design
In Solution Explorer, expand the properties node of the project.
In Solution Explorer, double-click the. settings file in which you want to add the new settings. The default name of this file is settings. settings.
In the settings designer, locate the settings you want to change, and then type a new value in the "value" column.
Change the setting value between application sessions
Sometimes, after compiling and deploying an application, you may need to change the setting value between application sessions. For example, you may need to change the connection string so that it points to the correct database location. Because the tool is not available after the application is compiled and deployed during design, you must manually change the Setting Value in the file.
To change the setting value between application sessions
Use Microsoft notepad or another text or XML editor to open the <assemblyname>. EXE. config file associated with the application.
Find the settings to change. It should look like the following example:
<Setting name = "setting" serializeas = "string"> <value> This is the set value </value> </setting>
Enter a new value for the settings and save the file.
Use settings during runtime
The runtime application can use the settings in the code. The setting values with application scopes can be accessed in read-only mode, while those with user scopes can be read and written. In C #, you can usePropertiesNamespace usage settings.
Read settings during runtime
You can use the properties namespace at runtime to read the application scope and user scope settings. PropertiesProperties. settings. DefaultThe object exposes all default settings of the project. When writing the code for using the settings, all the settings will appear in intelliisense and be strongly typed. Therefore, for example, if the set type isSystem. Drawing. Color, You do not need to perform forced type conversion to use this setting, as shown in the following example:
This. backcolor = properties. settings. Default. mycolor;
Save User settings at runtime
The Application Scope settings are read-only and can only be changed at design time or by modifying the <assemblyname>. EXE. config file between application sessions. However, user scope settings can be written at runtime, just like changing any attribute value. The new value is retained throughout the application session. You can call the settings. Save method to keep the changes made to user settings between application sessions. These settings are saved in the user. config file.
Steps for writing and maintaining user settings during runtime
Access user settings and assign new values to them, as shown in the following example:
Properties. settings. Default. mycolor = color. aliceblue;
To keep the changes made to user settings between application sessions, call the Save method, as shown in the following code:
Properties. settings. Default. Save ();
Use multiple group settings
In some cases, you may need to use multiple sets in the application. For example, if a group of settings in a developing application are expected to change frequently, it is wise to divide all the settings into a single file so that the corresponding files can be replaced in batches, other settings will not be affected. Visual Studio 2005 allows you to add multiple sets to a project. You can access the additional settings of each group through the setting objects generated in each node. For exampleSpecialsettingsInProperties. specialsettingsObject.
To add an additional Configuration Group
Select Add new item from the project menu ). The "Add new item" dialog box is displayed.
In the "Add new item" dialog box, select "settings file ).
In the "name" box, name the setting file, such as specialsettings. settings, and click "add" to add the file to the solution.
In Solution Explorer, drag the new settings file to the properties folder. In this way, you can use the new settings in the code.
Add and use settings in this file as in any other settings file. You can access the settings of this group through the properties. specialsettings object.
Conclusion
In this article, you have learned how to use settings in the C # application to store and manage settings from applications and users. You also learned how to add settings at design time, how to read and write settings at runtime, and how to merge multiple sets of additional settings into the application.
Http://msdn.microsoft.com/en-us/aa730869 (zh-CN, vs.80). aspx