zoukankan      html  css  js  c++  java
  • ini操作类

    public class IniHelper
    {
        // 声明INI文件的写操作函数 WritePrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        // 声明INI文件的读操作函数 GetPrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);

        private readonly int _retLength = 500;
        private readonly string _sPath = null;
        /// <summary>
        /// 初始化IniHelper
        /// </summary>
        /// <param name="path">ini文件保存路径</param>
        /// <param name="rl">默认500</param>
        public IniHelper(string path, int? rl = null)
        {
            this._sPath = path;
            this._retLength = rl.HasValue ? rl.Value : _retLength;
        }
        /// <summary>
        /// 设置Ini配置,默认配置节为Setting
        /// </summary>
        /// <param name="key">键名</param>
        /// <param name="value">键值</param>
        /// <param name="section">配置节</param>
        public void WriteValue(string key, string value, string section = "Setting")
        {
            // section=配置节,key=键名,value=键值,path=路径
            WritePrivateProfileString(section, key, value, _sPath);
        }
        /// <summary>
        /// 根据键名节点读取Ini配置,默认节点为Setting
        /// </summary>
        /// <param name="key">键名</param>
        /// <param name="section">配置节</param>
        /// <returns></returns>
        public string ReadValue(string key, string section = "Setting")
        {
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(_retLength);
            // section=配置节,key=键名,temp=上面,path=路径
            GetPrivateProfileString(section, key, "", temp, _retLength, _sPath);
            return temp.ToString();
        }
    }

     

    2017-07-10 09:42:56

  • 相关阅读:
    IE浏览器兼容问题
    sublime text3插件和快捷键
    CSS3高级
    盒子模型
    css3动画
    FreeBSD port安装 *** [checksum] Error code 1
    vs 2008设置vs6.0字体
    android 无法读取lua文件问题2
    u盘安装centos6 x8664
    cocos2dx lua 路径问题的一个bug (网络整理)
  • 原文地址:https://www.cnblogs.com/D-LuFei/p/7144362.html
Copyright © 2011-2022 走看看