Window setting. settings.

Source: Internet
Author: User

From: http://edu.admin5.com/article/20110124/0124431N2011.shtml

 

 

C # Application Program Set Source: Font: [large, medium, small]

C # application settings

Official reference: http://msdn.microsoft.com/zh-cn/library/k4s6c3a0 (V = vs.80). aspx

Use the application setting function provided by

    • Create a project
    • Choose [project]> [properties]
    • Select [settings]

You can manually add application settings.

After the application is added, the app. config file is automatically generated.

View sourceprint?
01 <?XML Version="1.0" Encoding="UTF-8"?>
02 <Configuration>
03 <Usersettings>
04 <Windowsapplication5.properties. Settings>
05 <Setting Name="Myset" Serializeas="String">
06 <Value> Testset_828 </Value>
07 </Setting>
08 <Setting Name="Formtitle" Serializeas="String">
09 <Value> Formtestdddd </Value>
10 </Setting>
11 </Windowsapplication5.properties. Settings>
12 </Usersettings>
13 </Configuration>

Differences between user and application

    • Application cannot update settings in the program. You can only manually change app. config or set project properties.
    • User allows you to change the settings in the program.

Vs also provides a shortcut to associate applications directly in the applicationsettings of the form control property.

The following lists precautions for using the vs built-in application.

    • If the range is application, it is read-only in the program. You can only modify the corresponding items of APP. config.
    • If the range is user and the program does not modify the value, modify the corresponding item of APP. config. The current value is set in APP. config during program access.
    • If the range is user and the value is modified in the program, the old value will be recorded in APP. config, and the future app. config setting will be invalid.

So where is the modified User value stored in the system?

Tested in c: \ Documents and Settings \ Administrator \ Local Settings \ Application Data \ phook \ windowsapplication5.exe _ url_nlwmvagksxwiigfpn5ymssypolictyn22ph \ 1.0.0.0 \ User. config

If you change the above app. config, the program will get the new value. It's strange why Microsoft is complicated.

Use in programs

View sourceprint?
1 // Obtain the value
2 Label1.text = properties. settings. Default. myset;
3 Label2.text = properties. settings. Default. MyApp;
4 // Modify the value. Only the user range can be modified.
5 Properties. settings. Default. myset ="Test1111";
6 Properties. settings. Default. Save ();

Summary

    • This setting is user-level if the range is user. The values obtained by running the program after logging on to different users are different.
    • If the program name is changed, it will have another set of APP. config.
    • Application is at the application level, and any application that opens the same program will be the same.

Custom app. config

You may want a config that has the same functions as the application, but supports program modification at the same time. The following are the implementation methods:

Create a project

Manually add app. config

The format is as follows:

View sourceprint?
1 <?XML Version="1.0" Encoding="UTF-8" ?>
2 <Configuration>
3 <Appsettings>
4 <Add Key="Y" Value="This is Y"/>
5 </Appsettings>
6 </Configuration>

ReferenceSystem. Configuration


Combine the operations on App. config into a class for convenient calling.

View sourceprint?
01 Using System;
02 Using System. Collections. Generic;
03 Using System. text;
04 Using System. configuration;
05   
06 // Note that you need to reference system. Configuration
07   
08 Public Class Appconfig
09 {
10 Private Static Configuration Config = configurationmanager. openexeconfiguration (configurationuserlevel. None );
11   
12 /// <Summary>
13 /// Obtain
14 /// </Summary>
15 /// <Param name = "key"> </param>
16 /// <Returns> </returns>
17 Public Static String Getvalue (String Key)
18 {
19 String Strreturn =Null;
20 If (Config. appsettings. settings [Key]! =Null)
21 {
22 Strreturn = config. etettings. settings [Key]. value;
23 }
24 Return Strreturn;
25 }
26   
27 /// <Summary>
28 /// Set
29 /// </Summary>
30 /// <Param name = "key"> </param>
31 /// <Param name = "value"> </param>
32 Public Static Void Setvalue (String Key,String Value)
33 {
34 If (Config. appsettings. settings [Key]! =Null)
35 {
36 Config. etettings. settings [Key]. value = value;
37 }
38 Else
39 {
40 Config. appsettings. settings. Add (Key, value );
41 }
42 Config. Save (configurationsavemode. modified );
43 }
44   
45 /// <Summary>
46 /// Delete
47 /// </Summary>
48 /// <Param name = "key"> </param>
49 Public Static Void Delvalue (String Key)
50 {
51 Config. appsettings. settings. Remove (key );
52 }
53 }

Usage

View sourceprint?
1 // Modify or add
2 Appconfig. setvalue ("Dtnow", Datetime. Now. millisecond. tostring ());
3 // Obtain
4 Label1.text = appconfig. getvalue ("Dtnow");

 

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.