1. 读取:首先在<appsetting>中加入
<appSettings>
<add key="FrontVersion" value="1.1.4"/>
</appSettings>
在代码中使用ConfigurationManager.AppSettings["FrontVersion"];来读取FrontVersion的值。
2. 修改web.config:
没有直接修改它的api函数,可以通过修改xml文件的方式修改它:
using System.Xml;
string sFile = Server.MapPath("web.config");
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(sFile);
XmlNode node = xmldoc.SelectSingleNode("//add[@key = 'SmtpServer']");
if (node != null)
{
node.Attributes["SmtpServer"].Value = "somethingelse";
xmldoc.Save(sFile);
}
这种修改web.config的方式会有缺陷,导致web application重新加载并且会话信息丢失。
如果必须要用程序修改web.config文件,建议建立一个新的文件进行读写。