赞
踩
乱码仅仅是因为编码规则导致
解码时对应文件的码制即可 ,
方法一 : 有可能出问题
- public class IniConfig
- {
- private string inipath = AppDomain.CurrentDomain.BaseDirectory + "Config1.ini";
- public bool CanRead()
- {
- if (File.Exists(inipath))
- {
- return true;
- }
- return false;
- }
- //声明API函数
-
- [DllImport("kernel32")]
- private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
- [DllImport("kernel32")]
- private static extern long WritePrivateProfileString(string section, string key, byte[] retVal, string filePath);
-
- [DllImport("kernel32")]
- private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
- [DllImport("kernel32")]
- private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
- /// <summary>
- /// 写入INI文件
- /// </summary>
- public void IniWriteValue(string Section, string Key, string Value)
- {
- WritePrivateProfileString(Section, Key, Value, inipath);
- }
- public void IniWriteValue(string Section, string Key, string Value, Encoding encoding)
- {
- byte[] retVal = encoding.GetBytes(Value);
- WritePrivateProfileString(Section, Key, retVal, inipath);
- }
- /// <summary>
- ///读INI文件: 含有中文时,请使用此方法 Encoding.Default
- /// </summary>
- public string IniReadValue(string Section, string Key, Encoding encoding)
- {
- try
- {
- byte[] Buffer = new byte[200];
- int bufLen = GetPrivateProfileString(Section, Key, "", Buffer, Buffer.GetUpperBound(0), inipath);
- string s = encoding.GetString(Buffer);
- s = s.Substring(0, bufLen);
- return s.Trim();
- }
- catch (Exception)
- {
- return "";
- }
- }
- public string IniReadValue(string Section, string Key)
- {
- try
- {
- StringBuilder temp = new StringBuilder(500);
- int i = GetPrivateProfileString(Section, Key, "", temp, 500, inipath);
- return temp.ToString();
- }
- catch (Exception)
- {
- return "";
- }
- }
- }

调用:
IniConfig ic = new IniConfig();
if (!ic.CanRead())
{ic.IniWriteValue("DataBase", "Connect", "你好啊",Encoding.UTF8);}
//有中文时指定编码格式
ic.IniReadValue("DataBase", "Connect", Encoding.UTF8);
//没有中文直接调用
ic.IniReadValue("DataBase", "Connect");
备注: 有可能出现 本机调试好用,目标机修改ini后就不正常了
原因:依旧是ini默认编码不一样,大概就是uft-8 与utf-8 bom的事情了
**************尚不知道怎么蹦出来的bom*******************
解决办法:在新电脑上重新生成ini文件,指定Encoding.UTF8就可以了
直接指定编码格式
方法二:
- public class IniFile
- {
- private string filePath = AppDomain.CurrentDomain.BaseDirectory + "Config.ini";
- private Dictionary<string, Dictionary<string, string>> sections = new Dictionary<string, Dictionary<string, string>>();
-
- public IniFile()
- {
- Load();
- }
-
- private void Load()
- {
- sections.Clear();
-
- if (!File.Exists(filePath))
- {
- return;
- }
- //使用 utf-8 格式读取
- using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8))
- {
- string line;
- string sectionName = null;
- while ((line = reader.ReadLine()) != null)
- {
- line = line.Trim();
- if (line.StartsWith(";") || line.Length == 0)
- {
- // 注释或空行,忽略
- continue;
- }
-
- if (line.StartsWith("[") && line.EndsWith("]"))
- {
- // 新节开始
- sectionName = line.Substring(1, line.Length - 2).Trim();
- sections[sectionName] = new Dictionary<string, string>();
- }
- else
- {
- // 键值对
- int index = line.IndexOf('=');
- if (index > 0)
- {
- string key = line.Substring(0, index).Trim();
- string value = line.Substring(index + 1).Trim();
- if (!string.IsNullOrEmpty(sectionName))
- {
- sections[sectionName][key] = value;
- }
- }
- }
- }
- }
- }
-
- private void Save()
- {
- //默认 utf-8 格式
- using (StreamWriter writer = new StreamWriter(filePath, false))
- {
- foreach (var section in sections)
- {
- writer.WriteLine($"[{section.Key}]");
- foreach (var setting in section.Value)
- {
- writer.WriteLine($"{setting.Key}={setting.Value}");
- }
- writer.WriteLine(); // 空行分隔不同的节
- }
- }
- }
-
- public string Get(string section, string key)
- {
- if (sections.ContainsKey(section) && sections[section].ContainsKey(key))
- {
- return sections[section][key];
- }
- return null;
- }
-
- public void Set(string section, string key, string value)
- {
- if (!sections.ContainsKey(section))
- {
- sections[section] = new Dictionary<string, string>();
- }
- sections[section][key] = value;
- Save();
- }
- }

调用:
IniFile iniFile = new IniFile();
//写入
iniFile.Set("fan", "aaaa", "你好啊1");
//读取
iniFile.Get("fan", "aaaa")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。