ini文件读取 IniHelper

 /// <summary>
    /// Ini文件操作类
    /// </summary>
    public class IniHelper
    {
        /// <summary>
        /// Ini文件完整路径
        /// </summary>
        public string IniPath { get; set; }

        /// <summary>
        /// 构造方法
        /// </summary>
        public IniHelper() { }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="path">Ini文件的完整路径,包括文件名</param>
        public IniHelper(string path)
        {
            IniPath = path;
        }

        #region win32函数
        /// <summary>
        /// 写操作
        /// </summary>
        /// <param name="section">Ini文件中的段落</param>
        /// <param name="key">Ini文件中的关键字</param>
        /// <param name="val">关键字的值</param>
        /// <param name="filePath">Ini文件的完整路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);

        /// <summary>
        /// 读操作
        /// </summary>
        /// <param name="section">Ini文件中的段落</param>
        /// <param name="key">Ini文件中的关键字</param>
        /// <param name="def">无法读取时的缺省值</param>
        /// <param name="retVal">读取的值</param>
        /// <param name="size">值的大小</param>
        /// <param name="filePath">Ini文件的完整路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
        #endregion

        /// <summary>
        /// Ini文件写入
        /// </summary>
        /// <param name="section">Ini文件段落</param>
        /// <param name="ident"></param>
        /// <param name="value">值</param>
        public void WriteValue(string section, string ident, string value)
        {
            bool b = WritePrivateProfileString(section, ident, value, IniPath);
            if (!b) throw new Exception("Ini文件写入错误");
        }

        /// <summary>
        /// 读取指定的Section和Ident的值
        /// </summary>
        /// <param name="section">Ini文件段落</param>
        /// <param name="ident">关键字</param>
        /// <returns>值</returns>
        public string ReadValue(string section, string ident)
        {
            return ReadValue(section, ident, "");
        }

        /// <summary>
        /// 读取指定的Section和Ident的值
        /// </summary>
        /// <param name="section">Ini文件段落</param>
        /// <param name="ident">关键字</param>
        /// <param name="defaultValue">读取失败时的默认值</param>
        /// <returns>值</returns>
        public string ReadValue(string section, string ident, string defaultValue)
        {
            byte[] buffer = new byte[65535];
            int bufLen = GetPrivateProfileString(section, ident, defaultValue, buffer, buffer.GetUpperBound(0), IniPath);
            string s = Encoding.GetEncoding(0).GetString(buffer);
            s = s.Substring(0, bufLen);
            return s.Trim();
        }

        /// <summary>
        /// 获取指定Section下的所有Ident
        /// </summary>
        /// <param name="section"></param>
        /// <param name="idents"></param>
        public void ReadSection(string section, StringCollection idents)
        {
            byte[] buffer = new byte[16384];
            int bufLen = GetPrivateProfileString(section, null, null, buffer, buffer.GetUpperBound(0), IniPath);

            GetStringsFromBuffer(buffer, bufLen, idents);
        }

        /// <summary>
        /// 解析Section
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="bufLen"></param>
        /// <param name="idents"></param>
        private static void GetStringsFromBuffer(byte[] buffer, int bufLen, StringCollection idents)
        {
            idents.Clear();
            if (bufLen != 0)
            {
                int start = 0;
                for (int i = 0; i < bufLen; i++)
                {
                    if ((buffer[i] == 0) && ((i - start) > 0))
                    {
                        string s = Encoding.GetEncoding(0).GetString(buffer, start, i - start);
                        idents.Add(s);
                        start = i + 1;
                    }
                }
            }
        }

        /// <summary>
        /// 获取Sections
        /// </summary>
        /// <param name="sections"></param>
        public void ReadSections(StringCollection sections)
        {
            byte[] buffer = new byte[65535];
            int bufLen = GetPrivateProfileString(null, null, null, buffer, buffer.GetUpperBound(0), IniPath);

            GetStringsFromBuffer(buffer, bufLen, sections);
        }

        /// <summary>
        /// 获取指定Section下的所有Value
        /// </summary>
        /// <param name="section"></param>
        /// <param name="values"></param>
        public void ReadSectionValues(string section, NameValueCollection values)
        {
            StringCollection idents = new StringCollection();
            ReadSection(section, idents);
            values.Clear();
            foreach (string ident in idents)
            {
                values.Add(ident, ReadValue(section, ident, ""));
            }
        }

        /// <summary>
        /// 清除指定的Section
        /// </summary>
        /// <param name="section"></param>
        public void EraseSection(string section)
        {
            bool b = WritePrivateProfileString(section, null, null, IniPath);
            if (!b) throw new Exception("无法清除Ini文件中的Section");
        }

        /// <summary>
        /// 删除指定Section下的指定键
        /// </summary>
        /// <param name="section"></param>
        /// <param name="ident"></param>
        public void DeleteIdent(string section, string ident)
        {
            bool b = WritePrivateProfileString(section, ident, null, IniPath);
            if (!b) throw new Exception("无法删除指定的Ident");
        }

        /// <summary>
        /// 检查指定的键是否有值
        /// </summary>
        /// <param name="section"></param>
        /// <param name="ident"></param>
        /// <returns></returns>
        public bool ValueExists(string section, string ident)
        {
            StringCollection idents = new StringCollection();
            ReadSection(section, idents);
            return idents.IndexOf(ident) > -1;
        }

        /// <summary>
        /// 执行完对Ini文件的操作后,
        /// 调用本方法更新缓冲区,
        /// 对于Win9X,执行此方法,
        /// 对于Win NT及更高的系统上,则不需执行此方法
        /// </summary>
        public void UpdateFile()
        {
            WritePrivateProfileString(null, null, null, IniPath);
        }

        /// <summary>
        /// 验证文件是否存在
        /// </summary>
        /// <returns></returns>
        public bool ExistIniFile
        {
            get
            {
                if (string.IsNullOrEmpty(IniPath)) 
                    throw new ArgumentException("IniPath");
                
                return File.Exists(IniPath);
            }
        }

        ~IniHelper()
        {
            UpdateFile();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值