app.config和配置文件的读写

app.config和配置文件的读写

ID:34721655

大小:73.68 KB

页数:4页

时间:2019-03-10

app.config和配置文件的读写_第1页
app.config和配置文件的读写_第2页
app.config和配置文件的读写_第3页
app.config和配置文件的读写_第4页
资源描述:

《app.config和配置文件的读写》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、App.config和配置文件的读写主要用来读取数据库连接,当然也可以设置其他项,废话不多说看代码usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Configuration;usingSystem.Collections;usingSystem.Xml;usingSystem.Windows.Forms;namespaceWindo{classAppClass1{//GetConfiguration#regionGetConfigurat

2、ion/**////

///取得appSettings里的值//////键///publicstaticstringGetConfiguration(stringkey){returnConfigurationSettings.AppSettings[key];}publicstaticstringGetConfigValue(){stringconn;XmlDocumentxDoc=newXmlDocumen

3、t();try{//加载app.config文件//xDoc.Load(Application.ExecutablePath+".config");//这个读取的是:项目名.exe.config文件xDoc.Load("D:\Demo\Windo\Windo\Windo\app.config");//这里是读取app.config文件//寻找add元素XmlElementxElem1=(XmlElement)xDoc.SelectSingleNode("/configuration/connectionStrings/add

4、");if(xElem1!=null){//返回add元素中connectionString属性值conn=xElem1.GetAttribute("connectionString");//MessageBox.Show(xElem1.GetAttribute("connectionString"));returnconn;}returnconn="读取错误!";}catch(Exception){conn="读取错误!";returnconn;}}publicstaticstringGetConfigString(stringke

5、y){////TODO:在此处添加构造函数逻辑//returnConfigurationSettings.AppSettings[key];}//写操作publicvoidSetValue(stringAppKey,stringAppValue){XmlDocumentxDoc=newXmlDocument();//获取可执行文件的路径和名称xDoc.Load(System.Windows.Forms.Application.ExecutablePath+".config");XmlNodexNode;XmlElementxElem1

6、;XmlElementxElem2;xNode=xDoc.SelectSingleNode("//appSettings");xElem1=(XmlElement)xNode.SelectSingleNode("//add[@key='"+AppKey+"']");if(xElem1!=null)xElem1.SetAttribute("value",AppValue);else{xElem2=xDoc.CreateElement("add");xElem2.SetAttribute("key",AppKey);xElem2.SetA

7、ttribute("value",AppValue);xNode.AppendChild(xElem2);}xDoc.Save(System.Windows.Forms.Application.ExecutablePath+".config");}}/////////

///没啥用,狗头军师老出错。///publicclassAppConfig{privateXmlDocumentDoc=newXmlDocument();privateAppDomainAd=AppDomain.CurrentDom

8、ain;publicAppConfig(){Doc.Load(Ad.SetupInformation.ConfigurationFile);}publicvoidLoadAppConfig(){Doc.Load(Ad.S

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。