加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

ASP。NET程序中动态更改web.config中的设置项目

发布时间:2023-08-01 15:16:28 所属栏目:Asp教程 来源:
导读:朋友们可以自行测试,我这里都没有问题了,鳖了一上午的问题总算解决了

using system;

using system.collections;

using system.componentmodel;

using system.data;

using system.drawing;
朋友们可以自行测试,我这里都没有问题了,鳖了一上午的问题总算解决了
 
using system;
 
using system.collections;
 
using system.componentmodel;
 
using system.data;
 
using system.drawing;
 
using system.web;
 
using system.web.sessionstate;
 
using system.web.ui;
 
using system.web.ui.webcontrols;
 
using system.web.ui.htmlcontrols;
 
using system.xml ;
 
 
namespace webapplication1
 
{
 
/// <summary>
 
/// summary description for webform1.
 
/// </summary>
 
public class webform1 : system.web.ui.page
 
{
 
protected system.web.ui.webcontrols.textbox textbox1;
 
protected system.web.ui.webcontrols.dropdownlist dropdownlist1;
 
protected system.web.ui.webcontrols.button button1;
 
public webform1()
 
{
 
page.init += new system.eventhandler(page_init);
 
}
 
private void page_load(object sender, system.eventargs e)
 
{
 
if(!page.ispostback)
 
{
 
//打开某文件(假设web。config在根目录中)
 
string filename=server.mappath("/") + @"/web.config";
 
xmldocument  xmldoc= new xmldocument();
 
xmldoc.load(filename);
 
xmlnodelist topm=xmldoc.documentelement.childnodes;
 
foreach(xmlelement element in topm)
 
{
 
if(element.name.tolower()=="appsettings")
 
{
 
xmlnodelist _node=element.childnodes;
 
if ( _node.count >0 )
 
{
 
dropdownlist1.items.clear();
 
foreach(xmlelement el in _node)
 
{
 
dropdownlist1.items.add(el.attributes["key"].innerxml);
 
}
 
}
 
}
 
}
 
}
 
}
 
private void page_init(object sender, eventargs e)
 
{
 
initializecomponent();
 
}
 
#region web form designer generated code
 
/// <summary>
 
/// required method for designer support - do not modify
 
/// the contents of this method with the code editor.
 
/// </summary>
 
private void initializecomponent()
 
{    
 
this.button1.click += new system.eventhandler(this.button1_click);
 
this.load += new system.eventhandler(this.page_load);
 
}
 
#endregion
 
private void button1_click(object sender, system.eventargs e)
 
{
 
string filename=server.mappath("/") + @"/web.config";
 
xmldocument  xmldoc= new xmldocument();
 
xmldoc.load(filename);
 
xmlnodelist topm=xmldoc.documentelement.childnodes;
 
foreach(xmlelement element in topm)
 
{
 
if(element.name.tolower()=="appsettings")
 
{
 
xmlnodelist _node=element.childnodes;
 
if ( _node.count >0 )
 
{
 
foreach(xmlelement el in _node)
 
{
 
if(el.attributes["key"].innerxml.tolower()==this.dropdownlist1.selecteditem.value.tolower())
 
{
 
el.attributes["value"].value=this.textbox1.text;
 
}
 
}
 
}
 
}
 
}
 
xmldoc.save(filename);
 
}
 
}
 
}
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章