asp.net read and modify config file implementation Code _ Practical skills

Source: Internet
Author: User
Tags connectionstrings
1. Add app.config files to the project:
Right-click the project name, select Add → add new item, and in the Add New Item dialog box that appears, select Add application configuration file, or if the project does not have a profile previously, 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 has been compiled, under the Bin\debuge file, there will be two profiles (take this project as an example), one named "JxcManagement.EXE.config" and the other named "JxcManagement.vshost.exe.config ”。 The first file is the configuration file that is actually used by the project, and changes made in the running of the program are saved here, and the second file is the original code "app.config" synchronization file that will not change in the program's operation.
2. ConnectionStrings configuration section:
Please note that if your SQL version is in the "localhost\sqlexpress" version, the default installation is a SQL Server instance named "Data Source=localhost", which you must change in the following instance One sentence is "Data source=localhost\sqlexpress;" And do not add a space on either side of the equals number.
<!--database connection string-->
Copy Code 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 configured for the entire program, and if it is configured for the current user, use the UserSettings configuration section in the same format as the following configuration writing requirements.
<!--inventory management system initialization required parameters-->
Copy Code code as follows:

<appSettings>
<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 app.config file read and write, referring to the network article: http://www.codeproject.com/csharp/SystemConfiguration.asp titled "Read/write App.config File with. NET 2.0 "article.
Note: To access the app.config file using the following code, you must also add a reference to System.Configuration.dll in the project, in addition to adding a reference system.configuration.
4.1 Read connectionstrings configuration section
Copy Code code as follows:

<summary>
ConnectionName return data connection string based on 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 Update connectionstrings configuration section
Copy Code code as follows:

<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 void Updateconnectionstringsconfig (String newName,
String newconstring,
String newprovidername)
{
BOOL ismodified = false; Log whether the connection string already exists
If the connection string you want to change already exists
if (Configurationmanager.connectionstrings[newname]!= null)
{
IsModified = true;
}
Create a new 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);
}
Adds a new connection string to the configuration file.
Config. CONNECTIONSTRINGS.CONNECTIONSTRINGS.ADD (mysettings);
To save changes to a configuration file
Config. Save (configurationsavemode.modified);
Force the connectionstrings configuration section of the configuration file to be loaded again
Configurationmanager.refreshsection ("connectionstrings");
}

4.3 Read appstrings configuration section
Copy Code code as follows:

<summary>
Returns the value entry for the appsettings configuration section in the *.exe.config file
</summary>
<param name= "Strkey" ></param>
<returns></returns>
private static string Getappconfig (String strkey)
{
foreach (string key in Configurationmanager.appsettings)
{
if (key = = strkey)
{
return Configurationmanager.appsettings[strkey];
}
}
return null;
}

4.4 Update connectionstrings configuration section
Copy Code code as follows:

<summary>
Add a pair of keys, value pairs to the appsettings configuration section in the *.exe.config file
</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.appsettings)
{
if (Key==newkey)
{
IsModified = true;
}
}

Open App.config of executable
Configuration config =
Configurationmanager.openexeconfiguration (Configurationuserlevel.none);
You are need to remove the "old" settings object before you can replace it
if (ismodified)
{
Config. AppSettings.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");
}


Copy Code code as follows:

///<summary>
///write key,value to XML file
///</summary>
///<param name= "Key" ></param>
///<param name= "Value" ></param>
public static void Saveconfig (String key,string Value)
{
XmlDocument doc = new XmlDocument ();
//Get the full path of the configuration file
String strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString () + "app.config";
Doc. Load (strFileName);
//Find all elements with name "add"
xmlnodelist nodes = doc. getElementsByTagName ("add");
for (int i = 0; i < nodes. Count; i++)
{
//Get the key property of the current element
XmlAttribute att = nodes[i]. attributes["Key"]; The
//determines whether the current element is a target element
if (ATT) based on the first property of the element. Value = = Key)
{
//Assign a value to the second property in the target element
Att = nodes[i]. attributes["value"];

Att. Value = value;
break;
}

//Save the changes above
Doc. Save (strFileName);

}
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.