C#实现一个读取ini文件的类

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace DeviceHandler
{
    /// <summary>
    /// ini配置文件帮助类
    /// </summary>
    public class ConfigHandler
    {
        [DllImport("kernel32")]
        static extern long WritePrivateProfileString(string section, string key, byte[] val, string filepath);
        [DllImport("kernel32")]
        static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
        string mSection;
        string mConfigPath;
        bool mNoSection = false;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName">文件名(例如:Config.ini)</param>
        /// <param name="section"></param>
        public ConfigHandler(string fileName, string section)
        {
            mConfigPath = Application.StartupPath + "\\" + fileName;
            mSection = section;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName">文件名(例如:Config.ini)</param>
        /// <param name="section"></param>
        public ConfigHandler(string fileName)
        {
                mConfigPath = fileName;
                mSection = "NOSECTION";
                mNoSection = true;
        }

        private void addSecton()
        {
            //try
            //{
                string[] lines = File.ReadAllLines(mConfigPath);
                string[] newLines = new string[lines.Length + 1];
                newLines[0] = "["+ mSection + "]";
                for (int i = 0; i < lines.Length; ++i)
                {
                    newLines[i + 1] = lines[i];
                }
                File.WriteAllLines(mConfigPath, newLines);

            //}
            //catch (Exception exp)
            //{
            //    Log.trace("addSecton Exception" + exp.ToString());
            //}

        }

        private void delSection()
        {
            //try
            //{
                string[] lines = File.ReadAllLines(mConfigPath);
                string[] newLines = new string[lines.Length - 1];
                for (int i = 0; i < lines.Length - 1; ++i)
                {
                    newLines[i] = lines[i + 1];
                }
                File.WriteAllLines(mConfigPath, newLines);

            //}
            //catch (Exception exp)
            //{
            //    Log.trace("addSecton Exception" + exp.ToString());
            //}
        }

        private static byte[] getBytes(string str)
        {
            return null == str ? null : Encoding.GetEncoding("utf-8").GetBytes(str);
        }

        /// <summary>
        /// 从ini文件中获取值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string getValue(string key)
        {
            if (mNoSection)
            {
                addSecton();
            }
            StringBuilder temp = new StringBuilder(1024);
            GetPrivateProfileString(mSection, key, "", temp, 1024, mConfigPath);

         //   Encoding.GetEncoding("utf-8").GetString(temp, 0, count).Trim();
            if (mNoSection)
            {
                delSection();
            }
            return temp.ToString().Trim();
        }

        /// <summary>
        /// 向ini文件写入值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void writeValue(string key, string value)
        {
            if (mNoSection)
            {
                addSecton();
            }
            WritePrivateProfileString(mSection, key, getBytes(value), mConfigPath);
            if (mNoSection)
            {
                delSection();
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值