基本信息
源码名称:c# 读取配置文件例子源码(适合各种config 以及自定义配置)
源码大小:2.31KB
文件格式:.cs
开发语言:C#
更新时间:2015-03-07
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍
适合各种config 以及自定义配置

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    /// <summary>
    ///  读写XML类
    ///  适合各种config 以及自定义配置
    /// </summary>
    public static class Global
    {
        /// <summary>
        /// 保存XML
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SaveConfig(string key, string value)
        {
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = System.AppDomain.CurrentDomain.BaseDirectory   "\\DLTiClient.exe.config";

            //加载Xml
            doc.Load(strFileName);

            //找出名称为“appSettings”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("appSettings");

            for (int i = 0; i < nodes[0].ChildNodes.Count; i  )
            {
                var node = nodes[0].ChildNodes[i];
                if (node.Attributes["key"].Value.ToString().Equals(key))
                {
                    node.Attributes["value"].Value = value;
                }
            }

            //保存上面的修改
            doc.Save(strFileName);
            return true;
        }

        /// <summary>
        /// 读取XML
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ReadConfig(string key)
        {
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = System.AppDomain.CurrentDomain.BaseDirectory   "\\DLTiClient.exe.config";

            //加载Xml
            doc.Load(strFileName);

            //找出名称为“appSettings”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("appSettings");

            for (int i = 0; i < nodes[0].ChildNodes.Count; i  )
            {
                var node = nodes[0].ChildNodes[i];
                if (node.Attributes["key"].Value.ToString().Equals(key))
                {
                    return  node.Attributes["value"].Value;
                }
            }

            return string.Empty;
        }
    }
}