1/// <summary>
 2  ///  修改web.config文件appSettings配置节中的Add里的value属性
         ///作者:sp
 3  /// </summary>
 4  /// <remarks>
 5  ///  注意,调用该函数后,会使整个Web Application重启,导致当前所有的会话丢失
 6  /// </remarks>
 7  /// <param name="key">要修改的键key</param>
 8  /// <param name="strValue">修改后的value</param>
 9  /// <exception cref="">找不到相关的键</exception>
10  /// <exception cref="">权限不够,无法保存到web.config文件中</exception>

11  public static void Modify(string key,string strValue)
12  {
13   string XPath="/configuration/appSettings/add[@key='?']";
14   XmlDocument domWebConfig=new XmlDocument();
15   
16   domWebConfig.Load( (HttpContext.Current.Server.MapPath("../web.config")) );
17   XmlNode addKey=domWebConfig.SelectSingleNode( (XPath.Replace("?",key)) );
18   if(addKey == null)
19   {
20    throw new ArgumentException("没有找到<add key='"+key+"' value=/>的配置节");
21   }

22   addKey.Attributes["value"].InnerText=strValue;
23   domWebConfig.Save( (HttpContext.Current.Server.MapPath("../web.config")) );
24   
25  }
 
posted on 2007-07-11 17:59  阳春  阅读(2275)  评论(0)    收藏  举报