1,创建新的类,以下仅为读取ini的函数,写部分可参考下文中的链接;
public static string ReadIni(string section, string key)
{
string IniFilePath = Environment.CurrentDirectory.ToString() + "\\config.ini";
//StringBuilder temp = new StringBuilder(255);
byte[] Buffer = new byte[255];
int i = GetPrivateProfileString(section, key, "", Buffer, Buffer.GetUpperBound(0), IniFilePath);
string s = Encoding.UTF8.GetString(Buffer, 0, i);
return s;
}
在类文件中,代码部分使用Encoding.UTF8.GetString()的格式;
2,API的接口参数如下:
[DllImport("kernel32")]
//private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
不使用StringBuilder ,因为实际测试发现中文会出现乱码;
参考连接:https://blog.csdn.net/wyl_tyrael/article/details/120291658 传送门
【好文共赏析!】