运维开发网

C# ini配置文件操作类

运维开发网 https://www.qedev.com 2020-04-27 12:21 出处:网络 作者:运维开发网整理
/// <summary> /// INI文件操作类 /// </summary> public class IniFileHelper { /// <summary> /// ini配置文件路径 /// </summary> private static string strIniFilePat
    /// <summary>
    /// INI文件操作类
    /// </summary>
    public class IniFileHelper {
        /// <summary>
        /// ini配置文件路径
        /// </summary>
        private static string strIniFilePath = AppDomain.CurrentDomain.BaseDirectory + "PrintDesigner.ini";

        // 返回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 IniFileHelper() {

        }

        /// <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 static string GetIniString(string section, string key, string strDefault = "") {
            StringBuilder sb = new StringBuilder(1000);
            long liRet = GetPrivateProfileString(section, key, strDefault, sb, sb.Capacity, strIniFilePath);
            return sb.ToString();
        }

        /// <summary>
        /// 获取ini配置文件中的整型值
        /// </summary>
        /// <param name="section">节名</param>
        /// <param name="key">键名</param>
        /// <param name="nDefault">默认值</param>
        /// <returns></returns>
        public static int GetIniInt(string section, string key, int nDefault) {
            return GetPrivateProfileInt(section, key, nDefault, strIniFilePath);
        }

        /// <summary>
        /// 往ini配置文件写入字符串
        /// </summary>
        /// <param name="section">节名</param>
        /// <param name="key">键名</param>
        /// <param name="val">要写入的字符串</param>
        /// <returns>成功true,失败false</returns>
        public static bool WriteIniString(string section, string key, string val) {
            long liRet = WritePrivateProfileString(section, key, val, strIniFilePath);
            return (liRet != 0);
        }

        /// <summary>
        /// 往ini配置文件写入整型数据
        /// </summary>
        /// <param name="section">节名</param>
        /// <param name="key">键名</param>
        /// <param name="val">要写入的数据</param>
        /// <returns>成功true,失败false</returns>
        public static bool WriteIniInt(string section, string key, int val) {
            return WriteIniString(section, key, val.ToString());
        }
    }

 

读取:

tbUserName.Text = IniFileHelper.GetIniString("Config", "UserName");

写入:

IniFileHelper.WriteIniString("Config", "UserName", User.username);
0

精彩评论

暂无评论...
验证码 换一张
取 消