在web.config里加了几个appsetting的值,想通过程序来动态修改,如何做?

解决方案 »

  1.   

    好象不可以吧?web.config是静态的……
      

  2.   

    //web.config修改appSettings;
    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=.../>的配置节");
    Response.Write("<script>alert (\"没有找到<add key='"+key+"' value=.../>的配置节\")</script>");
    return;
    }
    addKey.Attributes["value"].InnerText=strValue;
    domWebConfig.Save( (HttpContext.Current.Server.MapPath("web.config")) );
       
    }
      

  3.   

    对xml文件的操作一样吧,在下愚见
      

  4.   

    操作方式同XML.
    WEB.CONFIG本身也是一个XML文档.