How to modify the configuration information of app.config

How to modify the app.config configuration information collection

Most people who ask this question a bit more, in fact, .Net provides such features we can save our application setup information in the app.config userSettings node.

https://www.cnblogs.com/liningx/archive/2010/08/25/1808328.html

Specific steps:

1 Add the name of the variable you need to save. Right-click and select "Properties" in your project -> "Settings." Enter the variable name you need to set the interface. The variable UserName is added and ConnectionString, see below.

 

After the addition was complete save it, we will find the above content is actually stored in the userSetting node of the app.config.

  1. <?xml version="1.0" encoding="utf-8" ?> 
  2. <configuration
  3.     <configSections
  4.         <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
  5.             <section name="X.UserSettingDemo.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> 
  6.         </sectionGroup
  7.     </configSections
  8.     <! - saved here -> 
  9.     <userSettings
  10.         <X.UserSettingDemo.Properties.Settings
  11.             <setting name="ConnetionString" serializeAs="String"
  12.                 <value /> 
  13.             </setting
  14.             <setting name="UserName" serializeAs="String"
  15.                 <value /> 
  16.             </setting
  17.         </X.UserSettingDemo.Properties.Settings
  18.     </userSettings
  19. </configuration
 

Read and UserName added 2 stored in the app.config above, ConnectionString variable.

  1. using System; 
  2. using System.Windows.Forms; 
  3. namespace X.UserSettingDemo 
  4.     public partial class Form1 : Form 
  5.     { 
  6.         public Form1() 
  7.         { 
  8.             InitializeComponent(); 
  9.         } 
  10.         private void Form1_Load(object sender, EventArgs e) 
  11.         { 
  12.             this.InitApperance(); 
  13.         } 
  14.         private void btnSave_Click(object sender, EventArgs e) 
  15.         { 
  16.             this.SaveUserSetting(); 
  17.         } 
  18.         /// <summary> 
  19.         /// reading information from the configuration file to the initial value of the control. 
  20.         /// </summary> 
  21.         private void InitApperance() 
  22.         { 
  23.             this.txtUserName.Text = X.UserSettingDemo.Properties.Settings.Default.UserName; 
  24.             this.txtConnectionString.Text = X.UserSettingDemo.Properties.Settings.Default.ConnetionString; 
  25.         } 
  26.         /// <summary> 
  27.         /// Save the value of the control to the app.config file to use next time you open the program can be directly displayed or used. 
  28.         /// </summary> 
  29.         private void SaveUserSetting() 
  30.         { 
  31.             X.UserSettingDemo.Properties.Settings.Default.UserName = this.txtUserName.Text; 
  32.             X.UserSettingDemo.Properties.Settings.Default.ConnetionString = this.txtConnectionString.Text; 
  33.             X.UserSettingDemo.Properties.Settings.Default.Save(); 
  34.         } 
  35.     } 
  36. }  

Guess you like

Origin www.cnblogs.com/bwdblogs/p/10942546.html