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" ?> |
04 |
< Windowsapplication5.properties. Settings > |
05 |
< Setting Name = "Myset" Serializeas = "String" > |
06 |
< Value > Testset_828 </ Value > |
08 |
< Setting Name = "Formtitle" Serializeas = "String" > |
09 |
< Value > Formtestdddd </ Value > |
11 |
</ Windowsapplication5.properties. Settings > |
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?
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" ?> |
4 |
< Add Key = "Y" Value = "This is Y" /> |
ReferenceSystem. Configuration
Combine the operations on App. config into a class for convenient calling.
View sourceprint?
02 |
Using System. Collections. Generic; |
04 |
Using System. configuration; |
06 |
// Note that you need to reference system. Configuration |
08 |
Public Class Appconfig |
10 |
Private Static Configuration Config = configurationmanager. openexeconfiguration (configurationuserlevel. None ); |
15 |
/// <Param name = "key"> </param> |
16 |
/// <Returns> </returns> |
17 |
Public Static String Getvalue ( String Key) |
19 |
String Strreturn = Null ; |
20 |
If (Config. appsettings. settings [Key]! = Null ) |
22 |
Strreturn = config. etettings. settings [Key]. value; |
30 |
/// <Param name = "key"> </param> |
31 |
/// <Param name = "value"> </param> |
32 |
Public Static Void Setvalue ( String Key, String Value) |
34 |
If (Config. appsettings. settings [Key]! = Null ) |
36 |
Config. etettings. settings [Key]. value = value; |
40 |
Config. appsettings. settings. Add (Key, value ); |
42 |
Config. Save (configurationsavemode. modified ); |
48 |
/// <Param name = "key"> </param> |
49 |
Public Static Void Delvalue ( String Key) |
51 |
Config. appsettings. settings. Remove (key ); |
Usage
View sourceprint?
2 |
Appconfig. setvalue ( "Dtnow" , Datetime. Now. millisecond. tostring ()); |
4 |
Label1.text = appconfig. getvalue ( "Dtnow" ); |