ArticleDirectory
- Change existing settings during design
- Change the setting value between application sessions
- Read settings during runtime
- Save User settings at runtime
Set the release date in C #: 2006-10-09 | Updated on: 2006-10-09
Matt stoecker
Microsoft Corporation
Applicable:
| • |
Microsoft Visual C #2005 |
| • |
Microsoft. NET Framework 2.0 |
Abstract:Learn how to work with ApplicationsProgramAnd use the new functions in Visual C #2005.
Content on this page
|
Introduction |
|
Application and user settings |
|
Create new settings during design |
|
Use settings during runtime |
|
Use multiple group settings |
|
Conclusion |
Introduction
. Net
Framework 2.0
Value that allows you to create and access the value that is maintained between sessions executed by each application. These values are called settings. Setting can indicate user preferences or valuable information required by applications. For example, you can create a series
Column settings to store the user preferences of the application's color scheme. You can also store the connection string of the database used by the specified application. You can setCodeExternal applications are critical to messages
You can also create a configuration file that stores user preferences.
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.
Back to Top
Application and user settings
The setting has four attributes:
| • |
name: The name attribute is the name used to access the set value during runtime. |
| • |
type: the set "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: "value" indicates the value returned during access settings. The value is the type indicated by the "type" attribute. |
This
Most of these attributes are quite easy to understand. The concepts of "name", "type", and "value" should be well known to most programmers. "Scope"
(Scope) attributes need to be described. The setting has two possible scopes: Application Scope and user scope. Setting with Application Scope indicates that the application uses
But user-specific settings are usually not very important for actual applications, they are likely to be associated with preferences or other non-key values.
Application Scope setting and user-defined
The important difference between domain settings is that user scope settings are read/written at runtime and can be changed and saved in code. The application scope is set to read-only at runtime. Although it can be read
Yes. Setting with application scope can only be changed at design time or by manually modifying the setting file.
Back to Top
Create new settings during design
Yes
To 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. Must be specified for each new setting
"Name", "type", "Scope", and "value ). After the settings are created, you can use the mechanism described later in this article
Evaluate.
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. |
Figure1. Visual Studio 2005Set 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. |
Back to Top
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 modified between the design or application sessions.
<Assemblyname>. EXE. config
File. 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 is used 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 ();
|
Back to Top
Use multiple group settings
In
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 be changed frequently, it is wise to divide all the settings into a single file.
In this way, you can replace the corresponding files in batches without affecting other settings. Visual Studio 2005
You can add multiple group settings 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. |
Back to Top
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.