C # Read and modify the App. config file in the Project Instance

Source: Internet
Author: User
Tags connectionstrings

This article will talk about reading and modifying the App. config file in the C # project instance. The most important function of AppConfig is to unify the command line options and configuration file options into a data structure. Although this can cause complexity.

C # A project refers to a series of unique, complex and interrelated activities that have a clear goal or purpose and must be within a specific time, budget, or resource limit, completed according to specifications. Project parameters include project scope, quality, cost, time, and resources.

1. Add the app. config file to the project in the C # project instance:

Right-click the project name in the C # project instance and choose "add"> "Add new item". In the "Add new project" dialog box that appears, 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:

 

 

<?xmlversionxmlversion="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 -->
<ConnectionStrings>
<Clear/>
<Addnameaddname = "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/>
<Addkeyaddkey = "userName" value = ""/>
<Addkeyaddkey = "password" value = ""/>
<Addkeyaddkey = "Department" value = ""/>
<Addkeyaddkey = "returnValue" value = ""/>
<Addkeyaddkey = "pwdPattern" value = ""/>
<Addkeyaddkey = "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 ");
}


 

C # Read and modify the App. config file in the project instance.





Related Article

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.