1. Add the app. config file to the project:
Right-click the project name and select "add"> "Add new item". In the displayed "Add new item" dialog box, select "add application configuration file "; if the project has no configuration file before, the default file name is "app. config, and click OK ". The app. config file that appears in the designer view is:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
</Configuration>
After the project is compiled, there will be two configuration files under the binDebuge file (this project is used as an example), one is "JxcManagement. EXE. config" and the other 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 original code "app. config.
2. connectionStrings configuration section:
NOTE: If your SQL version is 2005 Express, the name of the SQL server instance is localhostSQLExpress by default. You must change "Data Source = localhost; "is" Data Source = localhostSQLExpress; ". 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:
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 -->
<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>
4. Read and Update app. config
For reading and writing the app. config File, refer to the network article: http://www.codeproject.com/csharp/ SystemConfiguration. asp 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>
/// ConnectionName returns the data connection string based on the connection string name
/// </Summary>
/// <Param name = "connectionName"> </param>
/// <Returns> </returns>
Private static string GetConnectionStringsConfig (string connectionName)
{
String connectionString =
ConfigurationManager. ConnectionStrings [connectionName]. ConnectionString. ToString ();
Console. WriteLine (connectionString );
Return connectionString;
}
4.2 connectionStrings configuration update
/// <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 ");
}
4.3 read the 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 string GetAppConfig (string strKey)
{
Foreach (string key in ConfigurationManager. receivettings)
{
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 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. 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 ("receivettings"