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

Source: Internet
Author: User

 

IntroductionI don't know how you saved the application configuration for the next time. I started learning about it. net, and stores application configurations in binary or XML format. In this way, it is quite troublesome to create a separate configuration class and write the configuration code. 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 usageFirst, create a WinForm project to design a form: Add a MyApp through the "Add new project" function. settings: make the following settings in the opened data table: in this way, you have set three different types of configuration variables a, B, and c. Now you can use them in the program: private void Form1_Load (object sender, EventArgs e) {numericUpDown1.Value = MyApp. default. a; textBox1.Text = MyApp. default. b; checkBox1.Checked = MyApp. default. c;} private void form=formclosing (object sender, FormClosingEventArgs e) {MyApp. default. a = numericUpDown1.Value; MyApp. default. B = TextBox1.Text; MyApp. Default. c = checkBox1.Checked; MyApp. Default. Save ();} through the above code, you can load and Save the configuration when the form is closed. Run screenshot: Support for complex data typesSettings supports the storage of complex data, such as Color and Size. You can choose from the options: But 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 must use other configuration schemes. Read and Write complex data typesTaking 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 to the form, and then modify the background code to highlight the added 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 you 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 = writable application = read-only "application" option should be used to set important variables necessary for some programs to run, such as navigation URL and mathematical operation parameters, these configuration items may cause problems if they are tampered with by the user, so they must be read-only. Data

Related Article

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.