) Windows form application reads and modifies the app. config file.

Source: Internet
Author: User

Address: http://space.itpub.net/12639172/viewspace-494054

Add the app. config file to the project:

Right-click the project name and select "add"> "Add new project". In the displayed "Add new project" dialog box, select "add application ".ProgramConfiguration file. If there is no configuration file in the project before, the default file name is "app. config" and click "OK ". Appears inDesignThe app. config file in the "app. config" view is:

 
<?Xmlversion= "1.0"Encoding= "UTF-8"?><Configuration></Configuration>

 

After the project is compiled, Run "bin \ users. config ". The first file is the configuration file actually used by the project. All changes made during the program running will be saved here. The second file is the originalCodeThe synchronization file of "app. config" will not be changed during the program running.

 

Connectionstrings configuration section:

NOTE: If yourSQLIf the version is 2005 Express, the default value isInstallWhen the SQL server instance name is localhost \ sqlexpress, you must change the following instance "Data Source = localhost;" to "Data Source = localhost \ sqlexpress ;", do not add spaces on both sides of the equal sign.

 

 
<! -- Database connection string --><Connectionstrings><Addname= "Conjxcbook"Connectionstring= "Data Source = localhost; initial catalog = jxcbook; user id = sa; Password = ********"Providername= "System. Data. sqlclient" /></Connectionstrings>
Appsettings configuration section

The appsettings configuration section is the configuration of the entire program. If you configure the current user, use the usersettings configuration section. The format is the same as the following configuration writing requirements.

 

 <! -- Parameters required for invoicing management system initialization -->       <  Appsettings  >           <  Clear   />           <  Addkey  = "Username"  Value  = ""   />           <  Addkey  = "Password"  Value  = ""  />           <  Addkey  = "Department"  Value  = ""   />           <  Addkey  = "Returnvalue"  Value  = ""   />           <  Addkey  = "Pwdpattern"  Value  = ""   />           <  Addkey = "Userpattern"  Value  = ""   />  </  Appsettings  > 
Read and Update app. config

For the read and write operations on the app. config file, refer to the networkArticle: Http://www.codeproject.com/csharp/systemconfiguration.asp?": read/write app. config file with. NET 2.0.

Note: To use the following code to access the app. config file, in addition to adding a reference to system. configuration, you must also add a reference to system. configuration. dll in the project.

Read connectionstrings configuration section
/// <Summary>/// Connectionname returns the data connection string based on the connection string name/// </Summary>/// <Param name = "connectionname"> </param>/// <Returns> </returns>Private Static StringGetconnectionstringsconfig (StringConnectionname ){StringConnectionstring = configurationmanager. connectionstrings [connectionname]. connectionstring. tostring (); console. writeline (connectionstring );ReturnConnectionstring ;}
Connectionstrings configuration section
 /// <Summary>  /// Update the connection string  /// </Summary> /// <Param name = "newname"> connection string name </param>  /// <Param name = "newconstring"> connection string content </param>  /// <Param name = "newprovidername"> data provider name </param>  Private   Static   Void Updateconnectionstringsconfig ( String Newname, String Newconstring, String Newprovidername ){ Bool Ismodified = False ; // Record whether the connection string already exists  // If the connection string to be modified already exists  If (Configurationmanager. connectionstrings [newname]! = Null ) {Ismodified = True ;} // Create a connection string instance Connectionstringsettings mysettings = New Connectionstringsettings (newname, newconstring, newprovidername ); // Open the executable configuration file *. EXE. config Configuration Config = configurationmanager. openexeconfiguration (configurationuserlevel. None ); // If the connection string already exists, delete it first  If (Ismodified) {config. connectionstrings. connectionstrings. Remove (newname );} // Add a new connection string to the configuration file. Config. connectionstrings. connectionstrings. Add (mysettings );// Save the changes made to the configuration file Config. Save (configurationsavemode. modified ); // Force re-load the connectionstrings configuration section of the configuration file Configurationmanager. refreshsection ( "Connectionstrings" );}
Read appstrings configuration section
/// <Summary>/// Return the value item in the configuration section of the ettings in *. EXE. config file./// </Summary>/// <Param name = "strkey"> </param>/// <Returns> </returns>Private Static StringGetappconfig (StringStrkey ){Foreach(StringKeyInConfigurationmanager. receivettings ){If(Key = strkey ){ReturnConfigurationmanager. configurettings [strkey];}Return Null;}
Connectionstrings configuration section
 /// <Summary>  /// In the *. EXE. config file, add a pair of key and value pairs to the deleettings configuration section. /// </Summary>  /// <Param name = "newkey"> </param>  /// <Param name = "newvalue"> </param>  Private   Static   Void Updateappconfig ( String Newkey, String Newvalue ){ Bool Ismodified = False ; Foreach ( String Key In Configurationmanager. receivettings ){ If (Key = newkey) {ismodified = True ;}} // Open app. config of executable Configuration Config = configurationmanager. openexeconfiguration (configurationuserlevel. None ); // You need to remove the old settings object before you can replace it  If (Ismodified) {config. deleettings. settings. Remove (newkey );} // Add an application setting. Config. appsettings. settings. Add (newkey, newvalue ); // Save the changes in APP. config file. Config. Save (configurationsavemode. modified ); // Force a reload of a changed section. Configurationmanager. refreshsection ("Appsettings" );}

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.