ASP.NET中动态修改web.config中的设置项目(CS页代码)
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
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