Use the configuration file (. settings,. config) to store application configurations

Source: Internet
Author: User
Introduction

I don't know how you saved the application earlier.ProgramConfiguration to be used for the next time I open it. I will study serialization when I start to learn. net, and save the application configuration in binary or XML format. In this way, each time you create a separate configuration class and write the read/write ConfigurationCode, Which is quite troublesome.

During this period, I also looked at the reading and writing methods of the. config file, and it was still very troublesome. It was better to serialize it myself.

Later, it was suddenly discovered that Microsoft had provided settings to define the content of the. config file and generate corresponding strong-type classes, which was extremely convenient to use ~, It feels like a place where Mars has lived for half a lifetime and suddenly found the earth *_*~

In view of the few and unknown information about settings on the internet, I will introduce the basic usage in this article, so that the birds will not take my broken path. Please go over the sky directly.

 

Basic usage

First, create a winform project to design such a form:

Use the "Add new item" function to add a MyApp. settings:

 

 

 

 

Make the following settings in the opened data table:

In this way, the configuration variables A, B, and C are set. Now they can be used in the program:

 

Private VoidForm1_load (ObjectSender,EventargsE)

{

Numericupdown1.Value= MyApp.Default.A;

Textbox1.Text= MyApp.Default.B;

Checkbox1.Checked= MyApp.Default.C;

}

 

Private VoidForm1_formclosing (ObjectSender,FormclosingeventargsE)

{

MyApp.Default.A=Numericupdown1.Value;

MyApp.Default.B=Textbox1.Text;

MyApp.Default.C=Checkbox1.Checked;

MyApp.Default.Save ();

}

 

With the above code, you can load and save the configuration when the form is open and closed.

Run screenshot:

 

Support for complex data types

Settings supports storing complex data, such as color and size:

Not all types are supported. For example, the fileinfo type does not exist, and the generic type does not. Custom types are not supported. If you need to store these special types, you have to use other configuration schemes.

 

Read and Write complex data types

Taking the arraylist type as an example, we can use it to record the time of every program shutdown based on the original program.

First, add a configuration variable D, whose type is system. Collections. arraylist:

Add a listboxt in the form and modify the background code:

The highlighted part is the new code.

Run multiple times to test:

You can see that each program close time is recorded and displayed.

 

"User" and "application"

You may notice that the "range" in the data table is all "user ":

What if I change it to another option, that is, "application?

When an error is reported, our configuration variables become read-only, so we can simply understand them:

User = read/write

Application = read-only

The "application" option should be used to set important variables necessary for running programs, such as navigation URLs and mathematical operation parameters. If these configuration items are tampered with by users, problems may occur, read-only.

 

Where is the data stored?

After compilation, you can find that there is a unique settingtest.exe. config in the debugdirectory. However, no matter how the program is operated, the default configuration described in this configuration file is always used.

In fact, the data is stored in the "C: \ Users \ skyd \ appdata \ Local \" Directory (I am Windows 7, XP or below may not be here)

I found the configuration file (user. config:

 

Conclusion

Although settings cannot support some complex and custom types, in most cases, it is more than enough to use it to store program configurations, do not duplicate the wheel.

Download Sample project: http://www.uushare.com/user/icesee/file/2017630

Download the XPS version of this article: http://www.uushare.com/user/icesee/file/2017629

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.