App.config和配置文件的读写

using System;

using System.Collections.Generic;

using System.Text;

using System.Configuration;

using System.Collections;

using System.Xml;

using System.Windows.Forms;

 

namespace Windo

{

    classAppClass1

    {

        //GetConfiguration#region GetConfiguration

        /**/

        ///<summary>

        ///取得appSettings里的值

        ///</summary>

        ///<paramname="key"></param>

        ///<returns></returns>

        publicstatic stringGetConfiguration(string key)

        {

         

            returnConfigurationSettings.AppSettings[key];

        }

        publicstatic string  GetConfigValue()

        {

 

            stringconn;

            XmlDocumentxDoc = new XmlDocument();

            try

            {

                //加载app.config文件

                //xDoc.Load(Application.ExecutablePath +".config");  //这个读取的是:项目名.exe.config文件

                xDoc.Load("D:\\Demo\\Windo\\Windo\\Windo\\app.config");  //这里是读取app.config文件

                //寻找add元素

                XmlElement xElem1 = (XmlElement)xDoc.SelectSingleNode("/configuration/connectionStrings/add");

                if (xElem1 != null)

                {

                    //返回add元素中connectionString属性值

                    conn =xElem1.GetAttribute("connectionString");

                   //MessageBox.Show(xElem1.GetAttribute("connectionString"));

                    return conn;

                }

                return conn = "读取错误!";

            }

            catch(Exception)

            {

                conn = "读取错误!";

                return conn;

            }

        }

       

        publicstatic stringGetConfigString(string key)

        {

            //

            //TODO: 在此处添加构造函数逻辑

            //

            returnConfigurationSettings.AppSettings[key];

        }

        //写操作

        publicvoid SetValue(stringAppKey, string AppValue)

        {

            XmlDocumentxDoc = new XmlDocument();

            //获取可执行文件的路径和名称

           xDoc.Load(System.Windows.Forms.Application.ExecutablePath+ ".config");

 

            XmlNodexNode;

            XmlElementxElem1;

            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.SetAttribute("value", AppValue);

                xNode.AppendChild(xElem2);

            }

           xDoc.Save(System.Windows.Forms.Application.ExecutablePath+ ".config");

        }

 

    }


///<summary>

///

///</summary>

    publicclass AppConfig

    {

        private XmlDocument Doc = newXmlDocument();

        privateAppDomain Ad = AppDomain.CurrentDomain;

        publicAppConfig()

        {

           Doc.Load(Ad.SetupInformation.ConfigurationFile);

        }

        publicvoid LoadAppConfig()

        {

           Doc.Load(Ad.SetupInformation.ConfigurationFile);

        }

        publicvoid SetAppValue(stringkey, string newValue)

        {

            Doc.SelectSingleNode("/configuration/appSettings/add[@key='"+ key + "']").Attributes["value"].Value = newValue;

        }

        publicvoid SaveAppConfig()

        {

           Doc.Save(Ad.SetupInformation.ConfigurationFile);

           Doc.Load(Ad.SetupInformation.ConfigurationFile);

        }

        publicstring GetAppValue(stringkey)

        {

            returnDoc.SelectSingleNode("/configuration/appSettings/add[@key='"+ key + "']").Attributes["value"].Value;

            //returnDoc.SelectSingleNode("/configuration/connectionStrings/add[@key='" +key + "']").Attributes["value"].Value;

        }

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值