版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、1.建立一個class,ReadWriteConfig.csusing System;using System.Configuration;using System.Reflection;using System.Web;using System.Xml;public enum ConfigFileType WebConfig, AppConfignamespace WebApplication1
2、160; /*/ <summary> / Summary description for ReadWriteConfig. / </summary> public class ReadWriteConfig
3、; public string docName = String.Empty; private XmlNode node=null; private int _configTyp
4、e; public int ConfigType get return _configType;
5、 set _configType=value; SetValue#region SetValue public bool SetValue(string key, strin
6、g value) XmlDocument cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc);
7、160; / retrieve the appSettings node node = cfgDoc.SelectSingleNode("/appSettings");
8、0; if( node = null )
9、60;throw new InvalidOperationException( "appSettings section not found"); try
10、0; / XPath select setting "add" element that contains this k
11、ey XmlElement addElem= (XmlElement)node.SelectSingleNode("/addkey='" +key +"'")
12、 if(addElem!=null) a
13、ddElem.SetAttribute("value",value); / not found, so we&
14、#160;need to add the element, key and value else
15、160; XmlElement entry = cfgDoc.CreateElement("add");
16、160;entry.SetAttribute("key",key); entry.SetAttribute("value",value);
17、160; node.AppendChild(entry);
18、160; /save it saveConfigDoc(cfgDoc,docName); return true;
19、160; catch
20、60; return false; #endregio
21、n saveConfigDoc#region saveConfigDoc private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath)
22、160; try XmlTextWriter writer = new XmlTextWriter( cfgDocPath ,
23、;null ); writer.Formatting = Formatting.Indented; cfgDoc.WriteTo(
24、 writer ); writer.Flush(); writer.Close();
25、0; return; catch
26、0; throw;
27、0; #endregion removeElement#region removeElement public bool removeElement (string elementKey)
28、0; try XmlDocument
29、160;cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc); / retrieve t
30、he appSettings node node = cfgDoc.SelectSingleNode("/appSettings");
31、 if( node = null ) throw new Inv
32、alidOperationException( "appSettings section not found");
33、60; / XPath select setting "add" element that contains this key to remove node.RemoveChild( no
34、de.SelectSingleNode("/addkey='" +elementKey +"'") ); saveConfigDoc(cfgDoc,docName); &
35、#160; return true; catch
36、60; return false;
37、60; #endregion modifyElement#region modifyElement public bool modifyElement (string elementKey)
38、60; try XmlDocument cfgDoc =
39、new XmlDocument(); loadConfigDoc(cfgDoc); / retrieve the appSettings
40、0;node node = cfgDoc.SelectSingleNode("/appSettings");
41、;if( node = null ) throw new InvalidOperationException(
42、 "appSettings section not found"); / XPath
43、160;select setting "add" element that contains this key to remove node.RemoveChild(node.SelectSingleNode("/ad
44、dkey='" +elementKey +"'"); saveConfigDoc(cfgDoc,docName); r
45、eturn true; catch
46、60; return false; #endregion
47、; loadConfigDoc#region loadConfigDoc private XmlDocument loadConfigDoc( XmlDocument cfgDoc )
48、 / load the config file if( Convert.ToInt32(ConfigType)=Convert.ToInt32(ConfigFileType.AppConfig)
49、 docName= (Assembly.GetEntryAssembly().GetName().Name;
50、160; docName += ".exe.config" else
51、160; cfgDoc.Load( docName ); return cfgDoc;
52、0; #endregion 2.建立測試頁面2.1 html<HTML> <HEAD> <title>WebForm1</title>
53、60; <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#">
54、60; <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content </HEAD> <body MS_POSITIONING=&
55、quot;GridLayout"> <form id="Form1" method="post" runat="server"> <asp:Button id="Button1" style=&quo
56、t;Z-INDEX: 101; LEFT: 296px; POSITION: absolute; TOP: 120px" runat="server" Text="刪除"></asp:Button>
57、160; <asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 232px; POSITION: absolute; TOP: 120px" runat="server"
58、 Text="新增"></asp:Button> <asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 104px; POSITION: absolute; TOP: 32px" r
59、unat="server">key</asp:Label> <asp:TextBox id="TextBox1" style="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 32px" runat="ser
60、ver"></asp:TextBox> <asp:Label id="Label2" style="Z-INDEX: 105; LEFT: 104px; POSITION: absolute; TOP: 96px" runat="server">value&
61、lt;/asp:Label> <asp:TextBox id="TextBox2" style="Z-INDEX: 106; LEFT: 168px; POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox&g
62、t; <asp:Button id="Button3" style="Z-INDEX: 107; LEFT: 360px; POSITION: absolute; TOP: 120px" runat="server"
63、160; Text="修改"></asp:Button> </form> </body></HTML>2.2 cs代碼public class
64、0; protected protected protected protected protected protect
65、ed protected private void Page_Load(object sender, System.EventArgs e)
66、60; Web Form Designer generated code#region Web Form Designer generated code overri
67、de protected void OnInit(EventArgs e) / / CODEGEN: This call is r
68、equired by the ASP.NET Web Form Designer. / InitializeComponent();
69、160; base.OnInit(e); /*/ <summary> / Required method for Designer sup
70、port - do not modify / the contents of this method with the code editor. / </summary>
71、60;private void InitializeComponent() this.Button1.Click += new System.EventHandler(this.Button1_Click);
72、0; this.Button2.Click += new System.EventHandler(this.Button2_Click); this.Button3.Click += new System.EventHandler(this.Button3_Click); this
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年滬科版選擇性必修三物理下冊階段測試試卷
- 二零二五年度建筑廢棄物資源化利用與木模板木方采購合同3篇
- 二零二五年度新能源汽車推廣應(yīng)用借款合同參考格式4篇
- 2025年度橋梁路面混凝土施工勞務(wù)合同范本4篇
- 2025年度農(nóng)用拖拉機(jī)租賃與農(nóng)田作業(yè)合同3篇
- 2025年度幕墻施工勞務(wù)分包合同施工質(zhì)量監(jiān)督與驗(yàn)收范本4篇
- 二零二五年度苗木新品種研發(fā)與推廣合作合同3篇
- 2025年度旅游地產(chǎn)開發(fā)貸款擔(dān)保合同4篇
- 二零二五年度廠房租賃與智慧城市建設(shè)合作合同范本3篇
- 護(hù)士長合同范本(2篇)
- GB/T 16288-2024塑料制品的標(biāo)志
- 麻風(fēng)病防治知識課件
- 干部職級晉升積分制管理辦法
- TSG ZF003-2011《爆破片裝置安全技術(shù)監(jiān)察規(guī)程》
- 護(hù)理服務(wù)在產(chǎn)科中的應(yīng)用課件
- 2024年代理記賬工作總結(jié)6篇
- 電氣工程預(yù)算實(shí)例:清單與計(jì)價樣本
- VOC廢氣治理工程中電化學(xué)氧化技術(shù)的研究與應(yīng)用
- 煤礦機(jī)電設(shè)備培訓(xùn)課件
- 高考寫作指導(dǎo)議論文標(biāo)準(zhǔn)語段寫作課件32張
- 2021年普通高等學(xué)校招生全國英語統(tǒng)一考試模擬演練八省聯(lián)考解析
評論
0/150
提交評論