App. config Configuration

Source: Internet
Author: User
Tags connectionstrings
Post: 1. Add to project App. config File: 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 the project has no configuration file before, the default file name is" App. config Click OK ". In the designer View App. config The file is: <? XML Version = "1.0"Encoding="UTF-8"?> < Configuration > </ Configuration > After the project is compiled Bin \ debuge File, two configuration files will appear ( This project is used as an example. ) , A Jxcmanagement. EXE. config ", The other name is" Jxcmanagement.vshost.exe. 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 originalCode" App. config The synchronization file will not be changed during the program running. 2. connectionstrings Configuration section: NOTE: If your SQL Version: 2005 expressconnect Version. SQL The name of the server instance Localhost \ sqlexpress , You must change the" Data Source = localhost; ":" Data Source = localhost \ sqlexpress; ", Do not add spaces on both sides of the equal sign. <! -- Database connection string --> < Connectionstrings > < Clear /> < Add Name = "Conjxcbook" Connectionstring = "Data Source = localhost; initial catalog = jxcbook; user id = sa; Password = ********" Providername = "System. Data. sqlclient"/> </ Connectionstrings > 3. appsettings Configuration section: Appsettings Configuration section is the configuration of the entire program. If you configure the current user, use Usersettings Configuration section in the same format as the following configuration writing requirements. <! -- Parameters required for invoicing management system initialization --> < Appsettings > < Clear /> < Add Key = "Username"Value=""/> < Add Key = "Password"Value=""/> < Add Key = "Department"Value=""/> < Add Key = "Returnvalue"Value=""/> < Add Key = "Pwdpattern"Value=""/> < Add Key = "Userpattern"Value=""/> </ Appsettings > 4. Read and update App. config For App. config File read/write, refer to the network Article : Http://www.codeproject.com/csharp/ systemconfiguration. asp is titled "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. 4.1 read connectionstrings configuration section /// <Summary> /// Returns the data connection string based on the connection string name connectionname /// </Summary> /// <Param name = "connectionname"> </param> /// <Returns> </returns> Private Static StringGetconnectionstringsconfig (StringConnectionname) { String Connectionstring = Configurationmanager. Connectionstrings [connectionname]. connectionstring. tostring (); Console. Writeline (connectionstring ); ReturnConnectionstring; } 4.2 connectionstrings configuration update /// <Summary> /// Update 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 VoidUpdateconnectionstringsconfig (StringNewname, StringNewconstring, StringNewprovidername) { BoolIsmodified =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 ConnectionstringsettingsMysettings = New Connectionstringsettings(Newname, newconstring, newprovidername ); // Open the executable configuration file *. EXE. config ConfigurationConfig = 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 changes to the configuration file Config. Save (Configurationsavemode. Modified ); // Force reload the connectionstrings configuration section of the configuration file Configurationmanager. Refreshsection ("Connectionstrings"); } 4.3 read the appstrings configuration section /// <Summary> /// Return the value in the configuration section of ettings in *. EXE. config. /// </Summary> /// <Param name = "strkey"> </param> /// <Returns> </returns> Private Static StringGetappconfig (StringStrkey) { Foreach(StringKeyIn Configurationmanager. Appsettings) { If(Key = strkey) { Return Configurationmanager. Deleetask[ strkey]; } } Return Null; } 4.4 connectionstrings configuration update /// <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 VoidUpdateappconfig (StringNewkey,StringNewvalue) { BoolIsmodified =False; Foreach(StringKeyIn Configurationmanager. Appsettings) { If(Key = newkey) { Ismodified =True; } } // Open app. config of executable ConfigurationConfig = Configurationmanager. Openexeconfiguration (Configurationuserlevel. None ); // You need to remove the old settings object before you can replace it If(Ismodified) { Config. Fig. 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"); }

Original article: http://blog.csdn.net/hbhecs/archive/2007/10/28/1851185.aspx

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.