我想通过建立一个.aspx页面,做为后台参数管理页面.来修改和管理web.config里的各项参数设置.
因为平时要改参数的话,都要去开服务器再打开web.config这个文件来改.用什么方法可以实现呢?最好能给个例子,万分感谢啊.

解决方案 »

  1.   

     其实web.config是一个xml文件,通过System.Xml.XmlDocument   来操作可以任意修改。例:web.config修改appSettings;
       
    public   void   test(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"))   );   
              
      }   
      

  2.   

    可以使用专门操作web.config的类   System.Web.Configuration.WebConfigurationManager 
    具体使用方法,可以参考 http://blog.csdn.net/zhanglei5415/archive/2007/10/16/1827515.aspx
      

  3.   

    楼上说的对,不应该使用操作XML来操作了
    使用XML来操作是1.1时代的事
    现在应该使用System.Web.Configuration.WebConfigurationManager来操作,简单方便。
    Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    //获取appsettings节点
    AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
    //在appsettings节点中添加元素
    appsection.Settings.Add("addkey1", "key1s value");
    appsection.Settings.Add("addkey2", "key2s value");
    config.Save();这样很方便的。修改也是很方便。//打开配置文件
    Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    //获取appsettings节点
    AppSettingsSection appsection = ( AppSettingsSection)config.GetSection("appSettings");
    //删除appsettings节点中的元素
    appsection.Settings.Remove("addkey1");
    //修改appsettings节点中的元素
    appsection.Settings["addkey2"].Value = "modify key2s value";
    config.Save();直接控制节点来操作。
      

  4.   

    2.0以上可以使用System.Web.Configuration.WebConfigurationManager