Asp.net reads and modifies the config file implementation code
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:
<? 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 original code "app. config.
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.
<! -- Database connection string -->
Copy the Code as follows:
<ConnectionStrings>
<Clear/>
<Addname = "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 -->
Copy the Code as follows:
<Deleetask>
<Clear/>
<Addkey = "userName" value = ""/>
<Addkey = "password" value = ""/>
<Addkey = "Department" value = ""/>
<Addkey = "returnValue" value = ""/>
<Addkey = "pwdPattern" value = ""/>
<Addkey = "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
Copy the Code as follows:
/// <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
Copy the Code as follows:
/// <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
Copy the Code as follows:
/// <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
Copy the Code as follows:
/// <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 ");
}
Copy the Code as follows:
/// <Summary>
/// Write the Key and Value to the XML file
/// </Summary>
/// <Param name = "Key"> </param>
/// <Param name = "Value"> </param>
Public static void SaveConfig (string Key, string Value)
{
XmlDocument doc = new XmlDocument ();
// Obtain the full path of the configuration file
String strFileName = AppDomain. CurrentDomain. BaseDirectory. ToString () + "App. config ";
Doc. Load (strFileName );
// Find all elements named "add"
XmlNodeList nodes = doc. GetElementsByTagName ("add ");
For (int I = 0; I <nodes. Count; I ++)
{
// Obtain the key attribute of the current element
XmlAttribute att = nodes [I]. Attributes ["key"];
// Determine whether the current element is a target element based on the first attribute of the element.
If (att. Value = Key)
{
// Assign values to the second attribute of the target Element
Att = nodes [I]. Attributes ["value"];
Att. Value = Value;
Break;
}
}
// Save the modification above
Doc. Save (strFileName );
}