• web.config中appSettings配置节修改的函数


     
    这个函数主要使用XmlDocument来解析web.config.并用SelectSingleNode()方法来定位要修改的配置节。要注意的是最后程序要Save(),所以,你的apsnet帐号必须对web.config拥有写权限.
    --------------------------------------------------------------------------------
    /// <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")) );

    }
  • 相关阅读:
    Eclipse修改背景颜色(豆沙绿)
    项目导入时报错:The import javax.servlet.http.HttpServletRequest cannot be resolved
    jdk1.7 环境变量配置
    Maven的安装、配置及使用入门
    tomcat端口作用
    《Maven实战》
    Maven 详解
    遍历Map的四种方法
    遍历properties文件
    题库终结
  • 原文地址:https://www.cnblogs.com/ganmk/p/1313492.html
Copyright © 2020-2023  润新知