正文

ASP.NET中动态修改web.config中的设置项目(CS页代码) 2006-05-18 17:41:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/Csharpsky/14311.html

分享到:

  1 using System;
  2 using
 System.Collections;
  3 using
 System.ComponentModel;
  4 using
 System.Data;
  5 using
 System.Drawing;
  6 using
 System.Web;
  7 using
 System.Web.SessionState;
  8 using
 System.Web.UI;
  9 using
 System.Web.UI.WebControls;
 10 using
 System.Web.UI.HtmlControls;
 11 using
 System.Xml ;
 12 

 13 
 14 namespace WebApplication1
 15 
{
 16 /// <summary>

 17 /// Summary description for WebForm1.
 18 /// </summary>

 19 public class WebForm1 : System.Web.UI.Page
 20 
{
 21 protected
 System.Web.UI.WebControls.TextBox TextBox1;
 22 protected
 System.Web.UI.WebControls.DropDownList DropDownList1;
 23 protected
 System.Web.UI.WebControls.Button Button1;
 24 

 25 public WebForm1()
 26 
{
 27 Page.Init += new
 System.EventHandler(Page_Init);
 28 
}
 29 

 30 private void Page_Load(object sender, System.EventArgs e)
 31 
{
 32 if(!
Page.IsPostBack)
 33 
{
 34 //打开某文件(假设WEB。CONFIG在根目录中)

 35 string filename=Server.MapPath("/"+ @"\web.config";
 36 XmlDocument  xmldoc= new
 XmlDocument();
 37 
xmldoc.Load(filename);
 38 

 39 XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
 40 foreach(XmlElement element in
 topM)
 41 
{
 42 if(element.Name.ToLower()=="appsettings"
)
 43 
{
 44 XmlNodeList _node=
element.ChildNodes;
 45 if ( _node.Count >0
 )
 46 
{
 47 
DropDownList1.Items.Clear();
 48 foreach(XmlElement el in
 _node)
 49 
{
 50 DropDownList1.Items.Add(el.Attributes["key"
].InnerXml);
 51 
}
 52 
}
 53 
}
 54 
}
 55 
}
 56 
}
 57 

 58 private void Page_Init(object sender, EventArgs e)
 59 
{
 60 
InitializeComponent();
 61 
}
 62 

 63 #region Web Form Designer generated code
 64 /// <summary>
 65 /// Required method for Designer support - do not modify
 66 ///
 the contents of this method with the code editor.
 67 /// </summary>

 68 private void InitializeComponent()
 69 
{    
 70 this.Button1.Click += new System.EventHandler(this
.Button1_Click);
 71 this.Load += new System.EventHandler(this
.Page_Load);
 72 

 73 }
 74 #endregion

 75 
 76 private void Button1_Click(object sender, System.EventArgs e)
 77 
{
 78 string filename=Server.MapPath("/"+ @"\web.config"
;
 79 XmlDocument  xmldoc= new
 XmlDocument();
 80 
xmldoc.Load(filename);
 81 

 82 XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
 83 foreach(XmlElement element in
 topM)
 84 
{
 85 if(element.Name.ToLower()=="appsettings"
)
 86 
{
 87 XmlNodeList _node=
element.ChildNodes;
 88 if ( _node.Count >0
 ) 
 89 
{
 90 foreach(XmlElement el in
 _node)
 91 
{
 92 if(el.Attributes["key"].InnerXml.ToLower()==this
.DropDownList1.SelectedItem.Value.ToLower())
 93 
{
 94 el.Attributes["value"].Value=this
.TextBox1.Text;
 95 
}
 96 
}
 97 
}
 98 
}
 99 
}
100 
xmldoc.Save(filename);
101 
}
102 
}
103 
}
104 

105 

阅读(3527) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册