C # configuration file read and modify

Source: Internet
Author: User

Configuration files are used in many cases, and the configuration file is divided into two types: the application configuration file and the Web configuration file.

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

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.
ConfigurationSettings also exist this problem, but I do not know how to refresh the node, hehe.
Old method: You crossing better use the "new method" below

Configuration file:

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

Background program is worth reading:

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

To modify the value of a configuration file:

1234567891011121314 /// <summary>/// 更新配置文件信息/// </summary>/// <param name="name">配置文件字段名称</param>/// <param name="Xvalue">值</param>privatevoid UpdateConfig(stringname,stringXvalue){    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:

12345678910111213141516171819202122232425 ///<summary>  ///向.config文件的appKey结写入信息AppValue   保存设置  ///</summary>  ///<param name="AppKey">节点名</param>  ///<param name="AppValue">值</param>Private voidSetValue(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:

system.configuration.configurationsettings.appsettings["Key"];
But now FrameWork2.0 has made it clear 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.

However, 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.

To update a configuration file:
Configuration CFA = configurationmanager.openexeconfiguration (Configurationuserlevel.none);
Add to

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

Modify

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

Last Call
CfA. Save ();
The current configuration file update was successful.

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

C # configuration file read and modify

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.