C#操作ini配置文件

原创
2015/05/22 12:32
阅读数 119
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace Qing.QConfigs
{
    /// <summary>
    /// ini配置
    /// </summary>
    public class IniConfig
    {
        private string configFile = "";
        /// <summary>
        /// 配置文件
        /// </summary>
        public string ConfigFile
        {
            set
            {
                if (string.IsNullOrEmpty(value)) configFile = "";
                else
                {
                    FileInfo info = new FileInfo(value);
                    configFile = info.FullName;
                }
            }
            get { return configFile; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public IniConfig() { }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configFile">配置文件</param>
        public IniConfig(string configFile)
        {
            ConfigFile = configFile;
        }

        /// <summary>
        /// 读取整型配置项
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键值</param>
        /// <param name="def">默认值,缺省为0</param>
        /// <returns>值</returns>
        public int Read(string section, string key, int def)
        {
            return GetPrivateProfileInt(section, key, def, ConfigFile);
        }
        /// <summary>
        /// 读取字符串配置项
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键值</param>
        /// <param name="def">默认值,缺省为空字符串</param>
        /// <param name="len">读取长度</param>
        /// <returns>值</returns>
        public string Read(string section, string key, string def, int len = 1024)
        {
            StringBuilder temp = new StringBuilder(len);
            GetPrivateProfileString(section, key, def, temp, len, ConfigFile);
            return temp.ToString();
        }

        /// <summary>
        /// 写入整型配置项
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键值</param>
        /// <param name="value">值</param>
        /// <returns>是否成功</returns>
        public bool Write(string section, string key, int value)
        {
            return WritePrivateProfileString(section, key, value.ToString(), ConfigFile);
        }
        /// <summary>
        /// 写入字符串配置项
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键值</param>
        /// <param name="value">值</param>
        /// <returns>是否成功</returns>
        public bool Write(string section, string key, string value)
        {
            return WritePrivateProfileString(section, key, value, ConfigFile);
        }

        /// <summary>
        /// 移除配置项
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键值</param>
        /// <returns>是否成功</returns>
        public bool Remove(string section, string key)
        {
            return WritePrivateProfileString(section, key, null, ConfigFile);
        }
        /// <summary>
        /// 移除节点
        /// </summary>
        /// <param name="section">节点</param>
        /// <returns>是否成功</returns>
        public bool Remove(string section)
        {
            return WritePrivateProfileString(section, null, null, ConfigFile);
        }

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
    }
}



展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部