如何用代码修改webconfig的appSettins节点内容??? RT 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 网摘:这个函数主要使用XmlDocument来解析web.config.并用SelectSingleNode()方法来定位要修改的配置节。要注意的是最后程序要Save(),所以,你的apsnet帐号必须对web.config拥有写权限. --------------------------------------------------------------------------------/// <summary>/// 修改web.config文件appSettings配置节中的Add里的value属性/// </summary>/// <res>/// 注意,调用该函数后,会使整个Web Application重启,导致当前所有的会话丢失/// </res>/// <param name="key">要修改的键key</param>/// <param name="strValue">修改后的value</param>/// <exception cref="">找不到相关的键</exception>/// <exception cref="">权限不够,无法保存到web.config文件中</exception>public 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")) );} 动态的修改了web.config,程序跑着没问题吗? octverve :按你给的做了,结果提示domWebConfig.Load( (HttpContext.Current.Server.MapPath("web.config")) ); 该行 未将对象引用设置到对象的实例。 最好不要这样做要动态修改一个xml文件好了。而不去修改webconfig这样的特殊的xml文件 关于aspx.cs转成dll文件的问题 求帮助 控制键盘复制 用png文件作图文验证就为什么更安全? 需要多长时间? 如何在Item_Command事件中取得模版列的值 Image控件没有NavigateURL,HyperLink控件可以设置ImageURL,但是不能设置Image的长宽,如何设置固定大小的带链接的图片 菜鸟请教一个js返回值的简单问题 页面上的一个操作要想两个表插入数据,并且向第2个表插入数据的时候要用到第一个表的递增ID 大家早上好!提一个javascript的问题 webchart大家用过吗?进来看看 能不能在服务端获取到客户端的屏幕分辨率
--------------------------------------------------------------------------------
/// <summary>
/// 修改web.config文件appSettings配置节中的Add里的value属性
/// </summary>
/// <res>
/// 注意,调用该函数后,会使整个Web Application重启,导致当前所有的会话丢失
/// </res>
/// <param name="key">要修改的键key</param>
/// <param name="strValue">修改后的value</param>
/// <exception cref="">找不到相关的键</exception>
/// <exception cref="">权限不够,无法保存到web.config文件中</exception>
public 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")) );}
按你给的做了,结果提示domWebConfig.Load( (HttpContext.Current.Server.MapPath("web.config")) );
该行 未将对象引用设置到对象的实例。
要动态修改一个xml文件好了。而不去修改webconfig这样的特殊的xml文件