.NET(C#) How to configure user preferences and save user settings

Recently, when developing software, it is necessary to save the user settings so that they can be used next time. After reading for a long time, it turns out that the .NET framework has its own setting function. The records are as follows:

One, the "Settings" page
  Use the "Settings" page of the project designer to specify the application settings for the project. Application settings provide the ability to dynamically store and retrieve property settings and other information for an application. These settings also allow you to maintain custom application and user preferences on client computers.

To access the Settings page, select the project node in Solution Explorer, then use the right-click context menu to select Properties. When the project designer appears, select the Settings tab.

Click the Settings tab to open the Settings page in the Project Designer:
  insert image description here
1.1 Title Bar
  The title bar at the top of the Settings page contains several controls:

Synchronize: Synchronize restores user-wide settings used during application runtime or debugging to default values ​​defined at design time. To restore the data, delete the runtime-generated application-specific files from disk instead of the project data.
Load Web Settings: Load Web Settings will display the Login dialog, allowing you to load the settings for an authenticated or anonymous user. This button is only enabled if Client Application Service on the Services page is enabled and a Web Settings Service Location is specified.
View Code: For C# projects, use the View Code button to view the code in the Settings.cs file. This file defines the Settings class, which can be used to handle specific events on the Settings object. In languages ​​other than Visual Basic, you must explicitly call the Save method of this wrapper class to persist user settings. This is usually done in the main form's Closing event handler. An example Save method call is shown below: Properties.Settings.Default.Save();
Access Modifier: The Access Modifier button specifies access to the Properties.Settings (for C#) or My.Settings (for Visual Basic) helper class Level, these classes are generated by Visual Studio in Settings.Designer.cs or Settings.Designer.vb.
For Visual C# projects, the access modifier can be "internal" or "public".

For Visual Basic projects, the access modifier can be "friend" or "public".

By default, the setting is Internal in C# and Friend in Visual Basic. If Visual Basic generates helper classes as "internal" or "friends," executable (.exe) applications cannot access resources and settings that have been added to class libraries (.dll files). If you need to share resources and settings in the class library, set the access modifier to "public".

1.2 Settings Network
  The Settings Grid is used to configure application settings. This grid includes the following columns:

Name: Enter a name for the application setting in this field
Type: Use the drop-down list to select the setting type. The most frequently used types will appear in the drop-down list, such as String, (Connection string), and System.Drawing.Font. You can choose Browse at the end of the list, and then select a type in the Select Type dialog box to select another type. When a type is selected, it is added to the common types in the drop-down list (current solution only).
Scope: Select Application or User. Application-wide settings, such as system fonts, are intended for use in user preferences. Users can change these settings at runtime.
Value: The data or value associated with the application setting. For example, if set to font, its value could be "Verdana, 9.75pt, style=bold".
Second, read settings at runtime (C#)
  Application-wide and user-wide settings can be read at runtime through the "properties" object. Properties objects expose all default settings for a project in the default namespace of the project in which they are defined, through the Properties.Settings.Default member.

Please access the corresponding settings through the Properties.Settings.Default member. The following example shows how to assign a setting named myColor to the BackColor property. This requires that you have previously created a settings file containing a setting named myColor of type System.Drawing.Color.

this.BackColor = Properties.Settings.Default.myColor;

Three, write user settings at runtime (C#)
  Application-wide settings are read-only and can only be changed at design time, or by changing the .config file between application sessions. But user-scoped settings can be written at runtime, just like any property value can be changed. The new value persists for the duration of the application session. Changes to settings can be persisted between application sessions by calling the Save method.

1. Access the setting and assign a new value to it, as in this example: Properties.Settings.Default.myColor = Color.AliceBlue;

2. If you want to preserve changes to settings between application sessions, call the Save method, as in this example: Properties.Settings.Default.Save();

4. Example
4.1 Create a simple form
insert image description here
4.2 Create user settings
insert image description here
4.3 Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace ComPort
{
    
    
    public partial class Form4 : Form
    {
    
    
        public Form4()
        {
    
    
            InitializeComponent();
        }
 
        private void Form4_Load(object sender, EventArgs e)
        {
    
    
            this.tb_color.Text = Properties.Settings.Default.color;
            this.tb_value.Text = Properties.Settings.Default.value;
            this.tb_name.Text = Properties.Settings.Default.name;
            this.nud.Value = Properties.Settings.Default.nud;
            this.tb_time.Text = Properties.Settings.Default.time;
        }
     <br>     // 控制第一列textbox的按钮
        private void OK_Click(object sender, EventArgs e)
        {
    
    
            Properties.Settings.Default.color = this.tb_color.Text;
            Properties.Settings.Default.value = this.tb_value.Text;
            Properties.Settings.Default.name = this.tb_name.Text;
            Properties.Settings.Default.nud = (int) this.nud.Value;
            Properties.Settings.Default.Save();
        }
<br>     // 控制第二列textbox的按钮
        private void btn_ok_Click(object sender, EventArgs e)
        {
    
    
            Properties.Settings.Default.time = this.tb_time.Text;
            Properties.Settings.Default.Save();
        }
    }
}

4.4 Running After  
  modification , close and restart, the system has recorded the last configuration value.

However, you cannot see the modified value in the app.config of the project, and the default value recorded here is the default value when it was created. Where does it exist? mine is existence

C:\Users\(user name)\AppData\Local\ComPort(project name)\ComPort.exe_Url_0zqu45wzw0sz20n5no0wbxd0nqhner5q\1.0.0.0\user.config

insert image description here

Guess you like

Origin blog.csdn.net/weixin_45499836/article/details/126312556