如何通过aspx页面修改web.config里的参数设置? 我想通过建立一个.aspx页面,做为后台参数管理页面.来修改和管理web.config里的各项参数设置.因为平时要改参数的话,都要去开服务器再打开web.config这个文件来改.用什么方法可以实现呢?最好能给个例子,万分感谢啊. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 其实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")) ); } 可以使用专门操作web.config的类 System.Web.Configuration.WebConfigurationManager 具体使用方法,可以参考 http://blog.csdn.net/zhanglei5415/archive/2007/10/16/1827515.aspx 楼上说的对,不应该使用操作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();直接控制节点来操作。 2.0以上可以使用System.Web.Configuration.WebConfigurationManager 怎样在执行某个文件时,让程序在文件中停几秒在执行下一步 如何用正则排除特殊符号 在VS机http://localhost:4256/里可以正常上传文件,在IIS即http://localhost/里提示“无法访问已关闭的文件” vs2008下怎么在页面事件中添加方法,只见有一个page_load啊? 问一个GridView的问题... 紧急求助 无法连接数据库,如有解决送200分 非常急的任务,数据导出成foxbase格式 数据库恢复报错,请大家帮忙看看! 在线等待。。。。关于Iframe的问题,,,高分相送 asp.net2.0窗体滚动层 大家是用的什么系统进行开发?
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")) );
}
具体使用方法,可以参考 http://blog.csdn.net/zhanglei5415/archive/2007/10/16/1827515.aspx
使用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();直接控制节点来操作。