自定义IniFile操作类

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

namespace Sisley
{
    public  class INIFileHepler
    {
       
        private string  Default_IniFileName = "Config.ini"; //INI文件名
        private string strIniFilePath;

        // 返回0表示失败,非0为成功  
        [DllImport("kernel32", CharSet = CharSet.Auto)]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        // 返回取得字符串缓冲区的长度  
        [DllImport("kernel32", CharSet = CharSet.Auto)]
        private static extern long GetPrivateProfileString(string section, string key, string strDefault, StringBuilder retVal, int size, string filePath);
        /*
         [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
         public static extern int GetPrivateProfileInt(string section, string key, int nDefault, string filePath);  
        */

        public INIFileHepler()  
        {
            this.strIniFilePath = Directory.GetCurrentDirectory() + "\\" +Default_IniFileName; 

        }  

 public INIFileHepler(string strIniFilePath)
        {
            if  ( Directory.Exists(strIniFilePath))
            {
                this.strIniFilePath = Directory.GetCurrentDirectory() + "\\" + Default_IniFileName;

            }

  this.strIniFilePath = strIniFilePath;
        }
       
        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="section">写入section</param>
        /// <param name="key">写入key</param>
        /// <param name="value">写入value</param>
        public void  WriteIniString(string section, string key, string value)  
        {
            try
            {
                WritePrivateProfileString(section, key, value, strIniFilePath);
            }
            catch
            {
                throw new Exception("配置文件不存在或权限不足导致无法写入");
            }
          
        }

        /// <summary>  
        /// 获取ini配置文件中的字符串  
        /// </summary>  
        /// <param name="section">节名</param>  
        /// <param name="key">键名</param>  
        /// <param name="strDefault">默认值</param>  
        /// <param name="retVal">结果缓冲区</param>  
        /// <param name="size">结果缓冲区大小</param>  
        /// <returns>返回值</returns>  
        public string  GetIniString(string section, string key, string strDefault, StringBuilder retVal, int size)
        {
            long liRet = GetPrivateProfileString(section, key, strDefault, retVal, size, strIniFilePath);
            //return (liRet >= 1);

            StringBuilder temp = new StringBuilder(1024);
            INIFileHepler.GetPrivateProfileString(section, key, strDefault, temp, 1024, strIniFilePath);
            return temp.ToString();
        }  


    }
}
View Code

相关文章:

  • 2022-01-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-05
相关资源
相似解决方案