111,119
社区成员
发帖
与我相关
我的任务
分享string connectionString = ConfigurationManager.ConnectionStrings["BuckMakerFT.Properties.Settings.BuckMakerFTConnectionString"].ConnectionString;<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="BuckMakerFT.Properties.Settings.BuckMakerFTConnectionString" connectionString="server=192.168.1.37\sqlexpress;Initial Catalog=BuckMakerFT;uid=;pwd=;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
/// <summary>
/// 修改web.config文件appSettings配置节中的Add里的value属性
/// </summary>
/// <remarks>
/// 注意,调用该函数后,会使整个Web Application重启,导致当前所有的会话丢失
/// </remarks>
/// <param name="key">要修改的键key</param>
/// <param name="strValue">修改后的value</param>
/// <exception cref="">找不到相关的键</exception>
/// <exception cref="">权限不够,无法保存到web.config文件中</exception>
public static void Modify(string key, string strValue)
{
string XPath = "/configuration/appSettings/add[@key='?']";
XmlDocument domWebConfig = new XmlDocument();
domWebConfig.Load((HttpContext.Current.Server.MapPath("Web.config")));
XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));
if (addKey == null)
{
throw new ArgumentException("没有找到<add key='" + key + "' value=.../>的配置节");
}
addKey.Attributes["value"].InnerText = strValue;
domWebConfig.Save((HttpContext.Current.Server.MapPath("Web.config")));
}
//修改appSettings节点中的元素
appSection.Settings["BuckMakerFT.Properties.Settings.BuckMakerFTConnectionString"].Value = "";
config.Save();