1. 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 ". The app. config file that appears in the designer view is:
View plaincopy to clipboardprint?
- <? Xmlversion ="1.0"Encoding ="UTF-8"?>
- <Configuration>
- </Configuration>
<? Xmlversion = "1.0" encoding = "UTF-8"?> <Br/> <configuration> <br/> </configuration> <br/>
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.
2. connectionstrings configuration section:
NOTE: If your SQL version is 2005 Express, the name of the SQL server instance is localhost \ sqlexpress by default. You must change "Data Source = localhost; "is" Data Source = localhost \ sqlexpress; ". Do not add spaces on both sides of the equal sign.
View plaincopy to clipboardprint?
-
- <! -- Database connection string -->
-
- <Connectionstrings>
-
- <Clear/>
- <Addname ="Conjxcbook"
-
- Connectionstring ="Data Source = localhost; initial catalog = jxcbook; user id = sa; Password = ********"
-
- Providername ="System. Data. sqlclient"/>
-
- </Connectionstrings>
<! -- Database connection string --> <br/> <connectionstrings> <br/> <clear/> <br/> <addname = "conjxcbook" <br/> connectionstring = "Data Source = localhost; initial catalog = jxcbook; user id = sa; Password = ********* "<br/> providername =" system. data. sqlclient "/> <br/> </connectionstrings> <br/>
3. 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.
View plaincopy to clipboardprint?
-
- <! -- Parameters required for invoicing management system initialization -->
-
- <Deleetask>
-
- <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>
<! -- Parameters required for the initialization of the invoicing Management System --> <br/> <appsettings> <br/> <clear/> <br/> <addkey = "username" value = ""/> <br/> <addkey = "password" value = ""/> <br/> <addkey = "department" value = ""/> <br/> <addkey = "returnvalue" value = ""/> <br/> <addkey = "pwdpattern" value = ""/> <br/> <addkey = "userpattern" value = ""/> <br/> </appsettings> <br/>
4. 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 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
View plaincopy to clipboardprint?
- /// <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;
-
- }
/// <Summary> <br/> // return the data connection string based on the connectionname of the connection string <br/> /// </Summary> <br/> /// <Param name = "connectionname"> </param> <br/> // <returns> </returns> <br/> Private Static string getconnectionstringsconfig (string connectionname) <br/>{< br/> string connectionstring = <br/> configurationmanager. connectionstrings [connectionname]. connectionstring. tostring (); <br/> console. writeline (connectionstring); <br/> return connectionstring; <br/>}< br/>
4.2 connectionstrings configuration update
View plaincopy to clipboardprint?
-
- /// <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 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
-
- Connectionstringsettings mysettings =
-
- NewConnectionstringsettings (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");
-
- }
/// <Summary> <br/> // update the connection string <br/> /// </Summary> <br/> /// <Param name = "newname "> connection string name </param> <br/> // <Param name =" newconstring "> connection string content </param> <br/> // <Param name = "newprovidername"> data provider name </param> <br/> Private Static void updateconnectionstringsconfig (string newname, <br/> string newconstring, <br/> string newprovidername) <br/>{< br/> bool ismodified = false; // record whether the connection string already exists <br/> // if the connection string to be modified has been saved In <br/> If (configurationmanager. connectionstrings [newname]! = NULL) <br/>{< br/> ismodified = true; <br/>}< br/> // create a connection string instance <br/> connectionstringsettings mysettings = <br/> New connectionstringsettings (newname, newconstring, newprovidername ); <br/> // open the executable configuration file *. EXE. config <br/> Configuration Config = <br/> configurationmanager. openexeconfiguration (configurationuserlevel. none); <br/> // if the connection string already exists, delete it first <br/> If (ismodified) <br/>{< br/> config. connectionstrings. connectionstrings. remove (newname); <br/>}< br/> // Add a new connection string to the configuration file. <br/> config. connectionstrings. connectionstrings. add (mysettings); <br/> // Save the changes made to the configuration file <br/> config. save (configurationsavemode. modified); <br/> // force the connectionstrings configuration section of the configuration file to be reloaded <br/> configurationmanager. refreshsection ("connectionstrings"); <br/>}< br/>
4.3 read the appstrings configuration section
View plaincopy to clipboardprint?
-
- /// <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. configurettings)
-
- {
-
- If(Key = strkey)
-
- {
-
- ReturnConfigurationmanager. configurettings [strkey];
- }
-
- }
-
- Return Null;
-
- }
/// <Summary> <br/> // return *. EXE. <br/> // </Summary> <br/> // <Param name = "strkey"> </param> <br/> // <returns> </returns> <br/> Private Static string getappconfig (string strkey) <br/>{< br/> foreach (string key in configurationmanager. appsettings) <br/>{< br/> If (Key = strkey) <br/>{< br/> return configurationmanager. appsettings [strkey]; <br/>}< br/> return NULL; <br/>}< br/>
4.4 connectionstrings configuration update
View plaincopy to clipboardprint?
-
- /// <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(StringKeyInConfigurationmanager. configurettings)
-
- {
-
- 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. 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");
-
- }
- Zookeeper ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Multi-layer configsections
Source Address
Http://gb2013.blog.163.com/blog/static/21735301201021253713453/
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "student" type = "system. configuration. dictionarysectionhandler"/>
</Configsections>
<Student>
<Add key = "name" value = "http://www.cnblogs.com/i80386/archive/2011/10/27/amily"/>
<Add key = "Age" value = "http://www.cnblogs.com/i80386/archive/2011/10/27/15"/>
<Add key = "sex" value = "http://www.cnblogs.com/i80386/archive/2011/10/27/female"/>
</Student>
</Configuration>
Note: configsections is equivalent to defining variables and must be placed in configuration
Idictionary istudent = (idictionary) configurationmanager. getsection ("student ");
String [] keys = new string [istudent. Keys. Count];
String [] values = new string [istudent. Keys. Count];
Istudent. Keys. copyto (keys, 0 );
Istudent. Values. copyto (values, 0 );
foreach (var key in keys)
{< br> console. writeline (key);
}< br> foreach (VaR value in values)
{< br> console. writeline (value);
}
foreach (var key in istudent. keys)
{< br> console. writeline ("{0 }:{ 1}", key, istudent [Key]);
}