很多時(shí)候,我們可能會(huì)在asp.net中動(dòng)態(tài)的修改配置文件,我們可以使用如下代碼中涉及到的類來完成配置文件,下面的代碼是修改connectionstring的例子:
System.Configuration.Configuration c = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
c.ConnectionStrings.ConnectionStrings.Clear();
ConnectionStringSettings s = new ConnectionStringSettings();
s.ProviderName = "System.Data.OleDb";
s.ConnectionString = "Provider=Microsoft.oledb.jet.4.0;data source=c:\\abc.mdb";
s.Name = "myaccess";
c.ConnectionStrings.ConnectionStrings.Add(s);
c.Save();
但是asp.net因?yàn)榘踩缘年P(guān)系, 不允許用Configuration的類對(duì)Web.config進(jìn)行修改,
只能用操作xml對(duì)象的形式進(jìn)行修改
XmlDocument doc = new XmlDocument();
doc.Load("Web.config");
XmlNode node = doc.DocumentElement.SelectSingleNode("appSettings");
node.SelectSingleNode("descendant::add[@key='Value_Inform']")。Attributes[1].Value = "111";
doc.DocumentElement.SelectSingleNode("appSettings")。InnerXml = node.InnerXml;
doc.Save("Web.config");
盡量不要修改web.config文件
web.config的改動(dòng)由aspnet_wp進(jìn)程監(jiān)視,如果有改動(dòng),因?yàn)闀?huì)導(dǎo)致web應(yīng)用程序重啟,降低性能,所有的Session會(huì)重置
但是不建議動(dòng)態(tài)修改web.config的值,因?yàn)闀?huì)導(dǎo)致web應(yīng)用程序重啟,降低性能。
![]() |
![]() | ![]() .. 定價(jià):¥225 優(yōu)惠價(jià):¥213 更多書籍 |
![]() | ![]() .. 定價(jià):¥133 優(yōu)惠價(jià):¥133.0 更多書籍 |