参考文章:
- https://blog.csdn.net/luthreestone/article/details/78119647
- https://blog.csdn.net/weixin_44713908/article/details/110134975
class IniFileUtil
{
public string m_sIniFile = ""; //INI文件名
[DllImport("kernel32.dll")]
private extern static int WritePrivateProfileSection(string section, string value, string fileName);
[DllImport("kernel32.dll")]
private static extern int WritePrivateProfileString(string section, string keyName, string value, string fileName);
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(string section, string keyName, string def, StringBuilder buffer, int size, string fileName);
[DllImport("kernel32.dll")]
private extern static int GetPrivateProfileString(string section, string keyName, string def, byte[] buffer, int size, string fileName);
[DllImport("kernel32.dll")]
private extern static int GetPrivateProfileSectionNames(byte[] buffer, int size, string fileName);
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileSection(string section, byte[] buffer, int size, string fileName);
[DllImport("kernel32.dll")]
private extern static int GetPrivateProfileSection(string section, StringBuilder buffer, int size, string fileName);
//声明读写INI文件的API函数
public IniFileUtil(string sIniFile)
{
m_sIniFile = sIniFile;
}
//类的构造函数,传递INI文件名
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, m_sIniFile);
}
//读INI文件
public string IniGetValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
GetPrivateProfileString(Section, Key, "", temp, 255, m_sIniFile);
return temp.ToString();
}
// 封装的方法中,最有价值的是获取所有Sections和所有的Keys,网上关于这个的代码大部分是错误的,这里给出一个正确的方法:
// 返回该配置文件中所有Section名称的集合
public ArrayList GetSections()
{
byte[] buffer = new byte[65535];
int rel = GetPrivateProfileSectionNames(buffer, buffer.GetUpperBound(0), m_sIniFile);
int iCnt, iPos;
ArrayList arrayList = new ArrayList();
string tmp;
if (rel > 0)
{
iCnt = 0; iPos = 0;
for (iCnt = 0; iCnt < rel; iCnt++)
{
if (buffer[iCnt] == 0x00)
{
tmp = System.Text.ASCIIEncoding.UTF8.GetString(buffer, iPos, iCnt - iPos).Trim();
iPos = iCnt + 1;
if (tmp != "")
arrayList.Add(tmp);
}
}
}
return arrayList;
}
// 获取节点的所有KEY值
public ArrayList GetKeys(string SectionName)
{
byte[] buffer = new byte[5120];
int rel = GetPrivateProfileString(SectionName, null, "", buffer, buffer.GetUpperBound(0), m_sIniFile);
int iCnt, iPos;
ArrayList arrayList = new ArrayList();
string tmp;
if (rel > 0)
{
iCnt = 0; iPos = 0;
for (iCnt = 0; iCnt < rel; iCnt++)
{
if (buffer[iCnt] == 0x00)
{
tmp = System.Text.ASCIIEncoding.UTF8.GetString(buffer, iPos, iCnt - iPos).Trim();
iPos = iCnt + 1;
if (tmp != "")
arrayList.Add(tmp);
}
}
}
return arrayList;
}
public Dictionary<string, string> GetKeysAndValues(string category)
{
byte[] buffer = new byte[2048];
GetPrivateProfileSection(category, buffer, 2048, m_sIniFile);
String[] tmp = System.Text.ASCIIEncoding.UTF8.GetString(buffer).Trim('\0').Split('\0');
Dictionary<string, string> result = new Dictionary<string, string>();
foreach (String entry in tmp)
{
if(entry[0] == '#')
{
continue;
}
string[] v = entry.Split('=');
result.Add(v[0], v[1]);
}
return result;
}
}