. NET operations on configuration file content

Source: Internet
Author: User

There are two types of configuration files: One is the configuration file for the WinForm application and one is the Web configuration file.

The biggest difference between the two profiles is that the Web's profile updates are updated from time to times, and the application's configuration files are not updated in realtime .

Refresh the application's configuration file after you update it

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

Operation Method:

Old method:

Configuration file:

<configuration>  <appSettings>    <add key="name" value=" SQL Server "/>  </appSettings></configuration>

Background program Read value:

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

To modify the value of a configuration file:

/// <summary>///Update profile Information/// </summary>/// <param name= "name" >configuration file Field name</param>/// <param name= "Xvalue" >value</param>Private voidUpdateconfig (stringNamestringXvalue) {XmlDocument doc=NewXmlDocument (); 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:

///<summary>  ///write information to a. config file Appkey appvalue save Settings///</summary>  ///<param name= "AppKey" >Node name</param>  ///<param name= "Appvalue" >value</param>PrivatevoidSetValue (String appkey,string appvalue) {Xmldocument XDoc=NewXmlDocument (); 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");

The above method has been explicitly stated in FrameWork2.0 that this property is obsolete. It is recommended to change to ConfigurationManager or WebConfigurationManager. And the AppSettings property is read-only and does not support modifying property values.

New method:

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

Background read value;

String str = configurationmanager.appsettings["Key"];

To update a configuration file:

Configuration CFA =configurationmanager.openexeconfiguration (Configurationuserlevel.none); //AddCFA. APPSETTINGS.SETTINGS.ADD ("Key","Name")//ModifyCFA. appsettings.settings["Browsedir"]. Value ="name";//SaveCFA. Save ();//refreshes, refreshes the named section, and reads it back from the disk the next time it is retrieved. Configurationmanager.refreshsection ("appSettings");//

WinForm application to see the modified values without exiting the program, you must remember to invoke the Refresh node!!!

. NET operations on configuration file content

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.