C # Read and Write Data in app. config

Source: Internet
Author: User

Read statement:

 

 String str = ConfigurationManager.AppSettings["DemoKey"];

 

Write statement:

 

 

 Configuration cfa =   ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
cfa.AppSettings.Settings["DemoKey"].Value = "DemoValue";
cfa.Save();

 

 

Configuration file content format: (app. config)

 

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DemoKey" value="*" />
</appSettings>
</configuration>

 

Several key sections marked with red pens are required

 

 

 System.Configuration.ConfigurationSettings.AppSettings["Key"];

 

However, FrameWork2.0 clearly indicates that this attribute is out of date. It is recommended to change to ConfigurationManager.

 

Or WebConfigurationManager. The AppSettings attribute is read-only and does not support modifying attribute values.

However, to call ConfigurationManager, you must first Add a reference to the system. configuration. dll assembly in the project.

(In solution manager, right-click the project name and choose add reference from the shortcut menu. net TablePage)
After adding a reference, you can use String str = ConfigurationManager. etettings ["Key"] to obtain the corresponding value.

Update the configuration file:

 

 Configuration cfa = ConfigurationManager.    OpenExeConfiguration(ConfigurationUserLevel.None);cfa.AppSettings.Settings.Add("key", "Name") ||   cfa.AppSettings.Settings["BrowseDir"].Value = "name";

 

And so on...
Last call
Cfa. Save ();
The current configuration file is updated successfully.

 

 

**************************************** **************************************** *********************************

 

Read and Write the configuration file app. config
The configuration file is provided in. Net, which allows us to Process configuration information in a very good way. This configuration is in XML format. In addition,. Net provides some functions to access this file.

1. Read configuration information
The specific content of a configuration file is as follows:

 <?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ConnenctionString" value="*" />
<add key="TmpPath" value="C:\Temp" />
</appSettings>
</configuration>

 

. Net provides a method to directly access the <etettings> (case-sensitive) element. There are many child elements in this element, and the names of these child elements are "add ", there are two attributes: "key" and "value ". In general, we can write our configuration information in this area and access it through the following methods:

 string ConString=System.Configuration    .ConfigurationSettings.AppSettings["ConnenctionString"];

 

The value of the key attribute of the child element following the deletemetadata, for example, deleetmetadata ["connenctionstring"], this is the subelement <add key = "ConnenctionString" value = "*"/>. The returned value is "*", that is, the value of the value attribute.

 

2. Set Configuration Information

If the configuration information is static, We can configure it manually. Pay attention to the format. If the configuration information is dynamic, we need to write a program to implement it. No configuration file is written in. Net. You can operate the configuration file by operating the XML file. The following is an example of writing a configuration file.

 Private void SaveConfig (string ConnenctionString)
{
XmlDocument doc = new XmlDocument ();
// Obtain the full path of the configuration file
String strFileName = AppDomain. CurrentDomain. BaseDirectory. ToString () + "Code.exe. 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 = "ConnectionString ")
{
// Assign values to the second attribute of the target Element
Att = nodes [I]. Attributes ["value"];
Att. Value = ConnenctionString;
Break;
}
}
// Save the modification 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.