读取配置文件Helper

3 篇文章 0 订阅

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;

namespace G3ERP.Win.MedicineProtectInterface
{
    public class Helper
    {
        [DllImport("kernel32")] // 读取配置文件的接口
        private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size, string filePath);

        //保存至临时文件
        public static void xmlSetValue(string AppKey, string AppValue)
        {
            XmlDocument xDoc = new XmlDocument();
            string strExecutablePath = AppDomain.CurrentDomain.BaseDirectory + "FileInfo";//保存在\bin\Debug中的profileInfo.xml中

            // 获取可执行文件的路径和名称
            xDoc.Load(strExecutablePath + ".xml");
            XmlNode xNode;
            XmlElement xElem1;
            xNode = xDoc.SelectSingleNode("//appSettings");
            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
            if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
            xDoc.Save(strExecutablePath + ".xml");
        }

        /// <summary>
        ///  读操作
        /// </summary>
        /// <param name="strExecutablePath"></param>
        /// <param name="appKey"></param>
        /// <returns></returns>
        public static string xmlGetValue(string appKey)
        {
            string strExecutablePath = AppDomain.CurrentDomain.BaseDirectory + "FileInfo";
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(strExecutablePath + ".xml");

                XmlNode xNode;
                XmlElement xElem;
                xNode = xDoc.SelectSingleNode("//appSettings");
                xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
                if (xElem != null)
                    return xElem.GetAttribute("value");
                else
                    return "";
            }
            catch (Exception ex)
            {
                return "";
            }
        }


        /// <summary>
        /// 记录log日志
        /// </summary>
        /// <param name="text"></param>
        public static void WriteLog(string text)
        {
            object obj = new object();
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log/Error.log";
            lock (obj)
            {
                using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8))
                {
                    sw.WriteLine(DateTime.Now.ToString() + "\t" + text);
                    sw.Flush();
                }
            }
        }

        // 读取配置文件的值
        public static string ProfileReadValue(string section, string key)
        {
            //配置文件位置
            string path = AppDomain.CurrentDomain.BaseDirectory + "license.ini";
            StringBuilder sb = new StringBuilder(255);
            GetPrivateProfileString(section, key, "", sb, 255, path);
            return sb.ToString().Trim();
        }
    }
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值