ini文件操作类

 INI文件其实是一种具有特定结构的文本文件,它的构成分为三部分,文件由若干个段落(section)组成,每个段落又分成若干个键(key)和值(value),结构如下:

[Section1]
key 1 = value2
key 1 = value2
……
[Section2]
key 1 = value1
key 2 = value2
……

 

    /// <summary> 
    /// ini文件帮助类
    /// </summary> 
    public class IniUtil
    {
        #region

        /// <summary>
        /// 获取所有段落名称
        /// </summary>
        /// <param name="lpszReturnBuffer">接受数据内存缓冲区指针</param>
        /// <param name="nSize">数据内存缓冲区最大大小</param>
        /// <param name="lpFileName">ini文件的路径</param>
        /// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string lpFileName);

        /// <summary>
        /// 获取值(String)
        /// </summary>
        /// <param name="lpAppName">段落名称</param>
        /// <param name="lpKeyName">键名称</param>
        /// <param name="lpDefault">找不到时返回的值</param>
        /// <param name="lpReturnedString">接受数据字符串</param>
        /// <param name="nSize">数据内存缓冲区最大大小</param>
        /// <param name="lpFileName">ini文件的路径</param>
        /// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);

        /// <summary>
        /// 获取值(char[])
        /// </summary>
        /// <param name="lpAppName">段落名称</param>
        /// <param name="lpKeyName">键名称</param>
        /// <param name="lpDefault">找不到时返回的值</param>
        /// <param name="lpReturnedString">接受数据字符串</param>
        /// <param name="nSize">数据内存缓冲区最大大小</param>
        /// <param name="lpFileName">ini文件的路径</param>
        /// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, [In, Out] char[] lpReturnedString, int nSize, string lpFileName);

        /// <summary>
        /// 获取值(IntPtr)
        /// </summary>
        /// <param name="lpAppName">段落名称</param>
        /// <param name="lpKeyName">键名称</param>
        /// <param name="lpDefault">找不到时返回的值</param>
        /// <param name="lpReturnedString">接受数据字符串指针</param>
        /// <param name="nSize">数据内存缓冲区最大大小</param>
        /// <param name="lpFileName">ini文件的路径</param>
        /// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, IntPtr lpReturnedString, uint nSize, string lpFileName);

        /// <summary>
        /// 获取值(Int)
        /// </summary>
        /// <param name="lpAppName">段落名称</param>
        /// <param name="lpKeyName">键名称</param>
        /// <param name="lpDefault">找不到时返回的值</param>
        /// <param name="lpFileName">ini文件的路径</param>
        /// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int lpDefault, string lpFileName);

        /// <summary>
        /// 获取指定段落
        /// </summary>
        /// <param name="lpAppName">段落名称</param>
        /// <param name="lpReturnedString">接受数据字符串指针</param>
        /// <param name="nSize">数据内存缓冲区最大大小</param>
        /// <param name="lpFileName">ini文件的路径</param>
        /// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);

        /// <summary>
        /// 获取值(String)(WritePrivateProfileString通过SetLastError返回错误)
        /// </summary>
        /// <param name="lpAppName">段落名称</param>
        /// <param name="lpKeyName">键名称</param>
        /// <param name="lpString">键对应值</param>
        /// <param name="lpFileName">ini文件的路径</param>
        /// <returns></returns>
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);

        #endregion

        private const int MaxSectionSize = 32767; // ini文件中一个段落(section)的最大大小。此属性定义缓冲区的最大大小,用于从ini文件中检索数据。这个值是win32函数GetPrivateProfileSectionNames()或GetPrivateProfileString ()允许的最大值。
        private string _path;//ini文件的路径

        /// <summary>
        /// ini文件的物理路径
        /// </summary>
        public string Path
        {
            get
            {
                return this._path;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path">ini文件的相对路径</param>
        public IniUtil(string path)
        {
            this._path = System.IO.Path.GetFullPath(path);
        }

        /// <summary>
        /// 获取指定段落指定键的值
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <param name="keyName">键名称</param>
        /// <param name="defaultValue">找不到时返回的值</param>
        /// <returns></returns>
        public string GetString(string sectionName, string keyName, string defaultValue)
        {
            if (sectionName == null)
                throw new ArgumentNullException("sectionName");

            if (keyName == null)
                throw new ArgumentNullException("keyName");

            StringBuilder result = new StringBuilder(MaxSectionSize);
            GetPrivateProfileString(sectionName, keyName, defaultValue, result, MaxSectionSize, _path);
            return result.ToString();
        }

        /// <summary>
        /// 获取指定段落指定键的值
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <param name="keyName">键名称</param>
        /// <param name="defaultValue">找不到时返回的值</param>
        /// <returns></returns>
        public int GetInt16(string sectionName, string keyName, short defaultValue)
        {
            int retval = this.GetInt32(sectionName, keyName, defaultValue);
            return Convert.ToInt16(retval);
        }

        /// <summary>
        /// 获取指定段落指定键的值
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <param name="keyName">键名称</param>
        /// <param name="defaultValue">找不到时返回的值</param>
        /// <returns></returns>
        public int GetInt32(string sectionName, string keyName, int defaultValue)
        {
            if (sectionName == null)
                throw new ArgumentNullException("sectionName");

            if (keyName == null)
                throw new ArgumentNullException("keyName");

            return GetPrivateProfileInt(sectionName, keyName, defaultValue, _path);
        }

        /// <summary>
        /// 获取指定段落指定键的值
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <param name="keyName">键名称</param>
        /// <param name="defaultValue">找不到时返回的值</param>
        /// <returns></returns>
        public double GetDouble(string sectionName, string keyName, double defaultValue)
        {
            string retval = this.GetString(sectionName, keyName, "");
            if (retval == null || retval.Length == 0)
            {
                return defaultValue;
            }
            return Convert.ToDouble(retval, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// 获取指定段落的所有键值对
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <returns></returns>
        public List<KeyValuePair<string, string>> GetSectionValuesAsList(string sectionName)
        {
            if (sectionName == null)
                throw new ArgumentNullException("sectionName");

            List<KeyValuePair<string, string>> result;
            string[] keyValuePairs;
            string key, value;
            int equalSignPos;
            IntPtr ptr = Marshal.AllocCoTaskMem(MaxSectionSize);
            try
            {
                int len = GetPrivateProfileSection(sectionName, ptr, MaxSectionSize, _path);
                keyValuePairs = ConvertNullSeperatedStringToStringArray(ptr, len);
            }
            finally
            {
                Marshal.FreeCoTaskMem(ptr);
            }

            result = new List<KeyValuePair<string, string>>(keyValuePairs.Length);
            for (int i = 0; i < keyValuePairs.Length; ++i)
            {
                equalSignPos = keyValuePairs[i].IndexOf('=');
                key = keyValuePairs[i].Substring(0, equalSignPos);
                value = keyValuePairs[i].Substring(equalSignPos + 1, keyValuePairs[i].Length - equalSignPos - 1);
                result.Add(new KeyValuePair<string, string>(key, value));
            }
            return result;
        }

        /// <summary>
        /// 获取指定段落的所有键值对
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <returns></returns>
        public Dictionary<string, string> GetSectionValues(string sectionName)
        {
            if (sectionName == null)
                throw new ArgumentNullException("sectionName");

            Dictionary<string, string> result;
            List<KeyValuePair<string, string>> keyValuePairs;

            keyValuePairs = this.GetSectionValuesAsList(sectionName);
            result = new Dictionary<string, string>(keyValuePairs.Count);

            foreach (KeyValuePair<string, string> keyValuePair in keyValuePairs)
            {
                if (!result.ContainsKey(keyValuePair.Key))
                {
                    result.Add(keyValuePair.Key, keyValuePair.Value);
                }
            }
            return result;
        }

        /// <summary>
        /// 获取指定段落的所有键名称
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <returns></returns>
        public string[] GetKeyNames(string sectionName)
        {
            if (sectionName == null)
                throw new ArgumentNullException("sectionName");

            string[] result;
            int len;
            IntPtr ptr = Marshal.AllocCoTaskMem(MaxSectionSize);
            try
            {
                len = GetPrivateProfileString(sectionName, null, null, ptr, MaxSectionSize, _path);
                result = ConvertNullSeperatedStringToStringArray(ptr, len);
            }
            finally
            {
                Marshal.FreeCoTaskMem(ptr);
            }
            return result;
        }

        /// <summary>
        /// 获取所有段落名称
        /// </summary>
        /// <returns></returns>
        public string[] GetSectionNames()
        {
            string[] result;
            int len;
            IntPtr ptr = Marshal.AllocCoTaskMem(MaxSectionSize);
            try
            {
                len = GetPrivateProfileSectionNames(ptr, MaxSectionSize, _path);
                result = ConvertNullSeperatedStringToStringArray(ptr, len);
            }
            finally
            {
                Marshal.FreeCoTaskMem(ptr);
            }
            return result;
        }

        /// <summary>
        /// 指针数据转换为字符串数组
        /// </summary>
        /// <param name="ptr">指向字符串数据的指针</param>
        /// <param name="valLength">指向指针的数据的长度</param>
        /// <returns></returns>
        private static string[] ConvertNullSeperatedStringToStringArray(IntPtr ptr, int valLength)
        {
            string[] result;
            if (valLength == 0)
            {
                result = new string[0];
            }
            else
            {
                string buff = Marshal.PtrToStringAuto(ptr, valLength - 1);//将缓冲区转换为字符串。长度减少1,这样我们就把第二个空移走了。
                result = buff.Split('\0');
            }
            return result;
        }

        /// <summary>
        /// 写入键值到指定段落
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <param name="keyName">键名称</param>
        /// <param name="value"></param> 
        private void WriteValueInternal(string sectionName, string keyName, string value)
        {
            if (sectionName == null)
                throw new ArgumentNullException("sectionName");

            if (keyName == null)
                throw new ArgumentNullException("keyName");

            if (value == null)
                throw new ArgumentNullException("value");

            if (!WritePrivateProfileString(sectionName, keyName, value, _path))
            {
                throw new System.ComponentModel.Win32Exception();
            }
        }

        /// <summary>
        /// 写入键值到指定段落
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <param name="keyName">键名称</param>
        /// <param name="value"></param>
        public void WriteValue(string sectionName, string keyName, string value)
        {
            this.WriteValueInternal(sectionName, keyName, value);
        }

        /// <summary>
        /// 写入键值到指定段落
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <param name="keyName">键名称</param>
        /// <param name="value"></param>
        public void WriteValue(string sectionName, string keyName, short value)
        {
            this.WriteValue(sectionName, keyName, (int)value);
        }

        /// <summary>
        /// 写入键值到指定段落
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <param name="keyName">键名称</param>
        /// <param name="value"></param>
        public void WriteValue(string sectionName, string keyName, int value)
        {
            this.WriteValue(sectionName, keyName, value.ToString(CultureInfo.InvariantCulture));
        }

        /// <summary>
        /// 写入键值到指定段落
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <param name="keyName">键名称</param>
        /// <param name="value"></param>
        public void WriteValue(string sectionName, string keyName, float value)
        {
            this.WriteValue(sectionName, keyName, value.ToString(CultureInfo.InvariantCulture));
        }

        /// <summary>
        ///写入键值到指定段落 
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <param name="keyName">键名称</param>
        /// <param name="value"></param>
        public void WriteValue(string sectionName, string keyName, double value)
        {
            this.WriteValue(sectionName, keyName, value.ToString(CultureInfo.InvariantCulture));
        }

        /// <summary>
        /// 删除指定段落的键
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        /// <param name="keyName">键名称</param>
        public void DeleteKey(string sectionName, string keyName)
        {
            if (sectionName == null)
                throw new ArgumentNullException("sectionName");

            if (keyName == null)
                throw new ArgumentNullException("keyName");

            this.WriteValueInternal(sectionName, keyName, null);
        }


        /// <summary>
        /// 删除指定段落
        /// </summary>
        /// <param name="sectionName">段落名称</param>
        public void DeleteSection(string sectionName)
        {
            if (sectionName == null)
                throw new ArgumentNullException("sectionName");

            if (!WritePrivateProfileString(sectionName, null, null, _path))
            {
                throw new System.ComponentModel.Win32Exception();
            }
        }

    }
posted @ 2019-08-17 11:15  唧唧复唧唧木兰当户织  阅读(1342)  评论(0编辑  收藏  举报