对web.config进行新增修改删除读取操作

1.建立一个class,ReadWriteConfig.cs

  1 using System;
2 using System.Configuration;
3 using System.Reflection;
4 using System.Web;
5 using System.Xml;
6 public enum ConfigFileType
7 {
8 WebConfig,
9 AppConfig
10 }
11
12 namespace WebApplication1
13 {
14 /// <summary>
15 /// Summary description for ReadWriteConfig.
16 /// </summary>
17 public class ReadWriteConfig
18 {
19 public string docName = String.Empty;
20 private XmlNode node=null;
21 private int _configType;
22 public int ConfigType
23 {
24 get{ return _configType; }
25 set{ _configType=value; }
26 }
27
28 #region SetValue
29 public bool SetValue(string key, string value)
30 {
31 XmlDocument cfgDoc = new XmlDocument();
32 loadConfigDoc(cfgDoc);
33 // retrieve the appSettings node
34 node = cfgDoc.SelectSingleNode("//appSettings");
35 if( node == null )
36 {
37 throw new InvalidOperationException( "appSettings section not found");
38 }
39 try
40 {
41 // XPath select setting "add" element that contains this key
42 XmlElement addElem= (XmlElement)node.SelectSingleNode("//add[@key='" +key +"']") ;
43 if(addElem!=null)
44 {
45 addElem.SetAttribute("value",value);
46 }
47 // not found, so we need to add the element, key and value
48 else
49 {
50 XmlElement entry = cfgDoc.CreateElement("add");
51 entry.SetAttribute("key",key);
52 entry.SetAttribute("value",value);
53 node.AppendChild(entry);
54 }
55 //save it
56 saveConfigDoc(cfgDoc,docName);
57 return true;
58 }
59 catch
60 {
61 return false;
62 }
63 }
64
65 #endregion
66
67 #region saveConfigDoc
68 private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath)
69 {
70 try
71 {
72 XmlTextWriter writer = new XmlTextWriter( cfgDocPath , null );
73 writer.Formatting = Formatting.Indented;
74 cfgDoc.WriteTo( writer );
75 writer.Flush();
76 writer.Close();
77 return;
78 }
79 catch
80 {
81 throw;
82 }
83 }
84
85 #endregion
86
87 #region removeElement
88 public bool removeElement (string elementKey)
89 {
90 try
91 {
92 XmlDocument cfgDoc = new XmlDocument();
93 loadConfigDoc(cfgDoc);
94 // retrieve the appSettings node
95 node = cfgDoc.SelectSingleNode("//appSettings");
96 if( node == null )
97 {
98 throw new InvalidOperationException( "appSettings section not found");
99 }
100 // XPath select setting "add" element that contains this key to remove
101 node.RemoveChild( node.SelectSingleNode("//add[@key='" +elementKey +"']") );
102 saveConfigDoc(cfgDoc,docName);
103 return true;
104 }
105 catch
106 {
107 return false;
108 }
109 }
110 #endregion
111
112 #region modifyElement
113 public bool modifyElement (string elementKey)
114 {
115 try
116 {
117 XmlDocument cfgDoc = new XmlDocument();
118 loadConfigDoc(cfgDoc);
119 // retrieve the appSettings node
120 node = cfgDoc.SelectSingleNode("//appSettings");
121 if( node == null )
122 {
123 throw new InvalidOperationException( "appSettings section not found");
124 }
125 // XPath select setting "add" element that contains this key to remove
126 node.RemoveChild(node.SelectSingleNode("//add[@key='" +elementKey +"']"));
127 saveConfigDoc(cfgDoc,docName);
128 return true;
129 }
130 catch
131 {
132 return false;
133 }
134 }
135 #endregion
136
137 #region loadConfigDoc
138 private XmlDocument loadConfigDoc( XmlDocument cfgDoc )
139 {
140 // load the config file
141 if( Convert.ToInt32(ConfigType)==Convert.ToInt32(ConfigFileType.AppConfig))
142 {
143 docName= ((Assembly.GetEntryAssembly()).GetName()).Name;
144 docName += ".exe.config";
145 }
146 else
147 {
148 docName=HttpContext.Current.Server.MapPath("web.config");
149 }
150 cfgDoc.Load( docName );
151 return cfgDoc;
152 }
153 #endregion
154 }
155 }

2.建立测试页面
2.1 html

 1 <HTML>
2 <HEAD>
3 <title>WebForm1</title>
4 <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
5 <meta name="CODE_LANGUAGE" Content="C#">
6 <meta name="vs_defaultClientScript" content="JavaScript">
7 <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
8 </HEAD>
9 <body MS_POSITIONING="GridLayout">
10 <form id="Form1" method="post" runat="server">
11 <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 296px; POSITION: absolute; TOP: 120px" runat="server"
12 Text="删除"></asp:Button>
13 <asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 232px; POSITION: absolute; TOP: 120px" runat="server"
14 Text="新增"></asp:Button>
15 <asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 104px; POSITION: absolute; TOP: 32px" runat="server">key</asp:Label>
16 <asp:TextBox id="TextBox1" style="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 32px" runat="server"></asp:TextBox>
17 <asp:Label id="Label2" style="Z-INDEX: 105; LEFT: 104px; POSITION: absolute; TOP: 96px" runat="server">value</asp:Label>
18 <asp:TextBox id="TextBox2" style="Z-INDEX: 106; LEFT: 168px; POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox>
19 <asp:Button id="Button3" style="Z-INDEX: 107; LEFT: 360px; POSITION: absolute; TOP: 120px" runat="server"
20 Text="修改"></asp:Button>
21 </form>
22 </body>
23 </HTML>

2.2 cs代码

 1 public class WebForm1 : System.Web.UI.Page
2 {
3 protected System.Web.UI.WebControls.Button Button1;
4 protected System.Web.UI.WebControls.Label Label1;
5 protected System.Web.UI.WebControls.TextBox TextBox1;
6 protected System.Web.UI.WebControls.Label Label2;
7 protected System.Web.UI.WebControls.TextBox TextBox2;
8 protected System.Web.UI.WebControls.Button Button3;
9 protected System.Web.UI.WebControls.Button Button2;
10
11 private void Page_Load(object sender, System.EventArgs e)
12 {
13
14 }
15
16 #region Web Form Designer generated code
17 override protected void OnInit(EventArgs e)
18 {
19 //
20 // CODEGEN: This call is required by the ASP.NET Web Form Designer.
21 //
22 InitializeComponent();
23 base.OnInit(e);
24 }
25
26 /// <summary>
27 /// Required method for Designer support - do not modify
28 /// the contents of this method with the code editor.
29 /// </summary>
30 private void InitializeComponent()
31 {
32 this.Button1.Click += new System.EventHandler(this.Button1_Click);
33 this.Button2.Click += new System.EventHandler(this.Button2_Click);
34 this.Button3.Click += new System.EventHandler(this.Button3_Click);
35 this.Load += new System.EventHandler(this.Page_Load);
36
37 }
38 #endregion
39
40 private void Button2_Click(object sender, System.EventArgs e)
41 {
42 //新增
43 ReadWriteConfig config = new ReadWriteConfig();
44 config.ConfigType = (int)ConfigFileType.WebConfig;
45 config.SetValue(this.TextBox1.Text,this.TextBox2.Text);
46 }
47
48 private void Button1_Click(object sender, System.EventArgs e)
49 {
50 //删除
51 ReadWriteConfig config = new ReadWriteConfig();
52 config.ConfigType = (int)ConfigFileType.WebConfig;
53 config.removeElement(this.TextBox1.Text);
54 }
55
56 private void Button3_Click(object sender, System.EventArgs e)
57 {
58 //修改
59 ReadWriteConfig config = new ReadWriteConfig();
60 config.ConfigType = (int)ConfigFileType.WebConfig;
61 config.SetValue(this.TextBox1.Text,this.TextBox2.Text);
62 }
63 }





posted @ 2012-04-05 12:47  烧点饭  阅读(234)  评论(0编辑  收藏  举报