asp.net C # Read configuration file Implementation method

Source: Internet
Author: User

Refresh after updating the application's configuration file

Configurationmanager.refreshsection ("appSettings");/refresh the named section and re-read it from disk the next time it is retrieved.

ConfigurationSettings also exists this problem, but I still do not know how to refresh the node, hehe.

Old method: You reader better use the "new method" below

Configuration file:

The code is as follows Copy Code

<configuration>
<appSettings>
<add key= "name" value= "I am a remote server"/>
</appSettings>
</configuration>

Background programs are worth reading:

The code is as follows Copy Code

String s=system.configuration.configurationsettings.appsettings["name"];

To modify the value of a configuration file:

  code is as follows copy code

///< Summary>

///Update profile information

///</summary>

///<param name= "name" > Profile field name </param

///<param name= "Xvalue" > Value </param>

private void Updateconfig (String name,string xvalue)

{

XmlDocument doc = new XmlDocument ();

Doc. Load (Application.executablepath + ". config");

XmlNode node = doc. selectSingleNode (@ "//add[@key = '" +name+ "]");

XmlElement ele = (xmlelement) node;

Ele. SetAttribute ("value", xvalue);

Doc. Save (Application.executablepath + ". config");

}

To insert a value into a configuration file:

The code is as follows Copy Code

<summary>

Write information to the Appkey knot of the. config file Appvalue Save settings

</summary>

<param name= "Appkey" > Festival naming </param>

<param name= "Appvalue" > Value </param>

Private void SetValue (String appkey,string appvalue)

{

XmlDocument xdoc=new XmlDocument ();

Xdoc.load (system.windows.forms.application.executablepath+ ". config");

XmlNode XNode;

XmlElement xElem1;

XmlElement xElem2;

Xnode=xdoc.selectsinglenode ("//appsettings");

xelem1= (XmlElement) xnode.selectsinglenode ("//add[@key = '" +appkey+ ")";

if (xelem1!=null)

Xelem1.setattribute ("value", appvalue);

Else

{

Xelem2=xdoc. CreateElement ("add");

Xelem2.setattribute ("Key", Appkey);

Xelem2.setattribute ("value", appvalue);

Xnode.appendchild (XELEM2);

}

Xdoc.save (system.windows.forms.application.executablepath+ ". config");

}

New method:

The code is as follows Copy Code
system.configuration.configurationsettings.appsettings["Key"];

But now FrameWork2.0 has made it clear that this attribute is obsolete. It was suggested that the change be ConfigurationManager or WebConfigurationManager. And the AppSettings property is read-only and does not support modifying property values.

However, to invoke ConfigurationManager you must first add a System.Configuration.dll assembly reference to the project. (Right-click the project name in Solution Manager and select Add Reference in the right menu to be found under. NET Tablepage) you can use String str = configurationmanager.appsettings["Key" after adding a reference To get the corresponding value.

Update configuration file:

The code is as follows Copy Code

Configuration CFA = configurationmanager.openexeconfiguration (Configurationuserlevel.none);
Add to

CfA. APPSETTINGS.SETTINGS.ADD ("Key", "Name")

Modify

CfA. appsettings.settings["Browsedir"]. Value = "name";

Last Call

The code is as follows Copy Code
CfA. Save ();

The current profile update was successful.

The code is as follows Copy Code
Configurationmanager.refreshsection ("appSettings");

Refreshes the named section and will re-read it from disk the next time it is retrieved. Remember that the application will refresh the node

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.