C# 读写INI文件中文乱码问题

C# 读写INI文件中文乱码问题

    • INIUtil操作类
    • 常见的读取ini文件的方式

INIUtil操作类


        //ini文件
        static string IniFileName = string.Format(@"{0}\{1}.ini", Application.StartupPath, Application.ProductName);      
       
        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);

        [DllImport("kernel32.dll")]
        private static extern long WritePrivateProfileString(string section, byte[] key, byte[] val, string filePath);

        /// 
        /// 指定节点 指定键写入值
        /// 
        /// 节点
        /// 
        /// 
        /// 
        public static bool WriteIni(string Section, string Key, string Value)
        {
            try
            {
                WritePrivateProfileString(Section, Encoding.UTF8.GetBytes(Key), Encoding.UTF8.GetBytes(Value), IniFileName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool WriteIni(string Key, string Value)
        {
            try
            {
                return  WriteIni("Config", Key, Value);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static string ReadIni(string Section, string Key)
        {
            try
            {
                byte[] Buffer = new byte[1024];
                int bufLen = GetPrivateProfileString(Section, Key, "", Buffer, Buffer.GetUpperBound(0), IniFileName);
                string s = Encoding.UTF8.GetString(Buffer, 0, bufLen);
                return s;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }

        /// 
        /// 读取指定节点 指定键的值
        /// 
        /// 节点
        /// 
        /// ini文件
        /// 
        public static string ReadIni(string Section, string Key, string FileName)
        {
            try
            {
                byte[] Buffer = new byte[1024];
                int bufLen = GetPrivateProfileString(Section, Key, "", Buffer, Buffer.GetUpperBound(0), FileName);
                string s = Encoding.UTF8.GetString(Buffer, 0, bufLen);
                return s;
            }
            catch (Exception ex)
            {
      		   return string.Empty;
            }
        }

        public static string ReadIni(string Key)
        {
            try
            {
                byte[] Buffer = new byte[1024];
                int bufLen = GetPrivateProfileString("Config", Key, "", Buffer, Buffer.GetUpperBound(0), IniFileName);
                string s = Encoding.UTF8.GetString(Buffer, 0, bufLen);//使用utf-8编码读取 解决乱码问题             
                return s;
            }
            catch (Exception ex)
            {            
                return string.Empty;
            }
        }

以上,关键点在于 读写都要用字节的方式,并且指定编码格式UTF-8

不过此方式会存在“后遗症”,那就是ini文件不能直接用记事本来编辑,或者说,最后的ini文件必须要指定编码格式为UTF-8,否则读取不到内容,一般可以使用notepad++来编辑ini文件。

常见的读取ini文件的方式

下面是常见的读取ini文件的方式,这样的方式对中文并不友好。


        [DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileStringA")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileStringA")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

你可能感兴趣的:(C#,c#)