web.config中appSettings配置节的函数

/// <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 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"));
}

相关文章:

  • 2022-01-19
  • 2021-09-11
  • 2022-12-23
  • 2022-02-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2021-10-13
  • 2022-01-16
  • 2022-12-23
  • 2021-07-31
相关资源
相似解决方案