public class IniFileHelper
{
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string sectionName, string key, string defaultValue, byte[] returnBuffer, int size, string filePath);
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string sectionName, string key, string value, string filePath);
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpszReturnBuffer, int nSize, string lpFileName);
public static string GetValue(string sectionName, string key, string filePath)
{
filePath = GetAsbPath(filePath);
byte[] array = new byte[2048];
int btsNu = GetPrivateProfileString(sectionName, key, string.Empty, array, array.Length, filePath);
return Encoding.Default.GetString(array, 0, btsNu);
}
/// <summary>
/// 获取配置文件中的所有分组和键值对
/// </summary>
/// <param name="filePath"></param>
/// <returns>如果读取数据失败返回null</returns>
public static Dictionary<string, List<ValueTuple<string, string>>> GetAllValues(string filePath)
{
//1.获取绝对路径
filePath = GetAsbPath(filePath);
//2.根据路径读取数据,并判断读取数据是否成功
if (!File.Exists(filePath))
{
return null;
}
//3. 获取的到的字符串
string readStr = string.Empty;
try
{
readStr = File.ReadAllText(filePath, Encoding.Default);
}
catch
{
return null;
}
if (string.IsNullOrEmpty(readStr) || !readStr.Contains("\r\n"))
{
return null;
}
//4. 分割字符串
string[] readStrSplicArr = readStr.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
Dictionary<string, List<ValueTuple<string, string>>> resultDic = new Dictionary<string, List<(string, string)>>();
string groupName = string.Empty;
List<ValueTuple<string, string>> val = null;
string[] valArr = null;
foreach (string itemStr in readStrSplicArr)
{
//4.1 判断是否是组名称, 特点是否包含等号
if (!itemStr.Contains("="))
{
if (valArr != null && !resultDic.ContainsKey(groupName))
{
resultDic.Add(groupName, val);
}
groupName = itemStr.Trim(']').Trim('[');
val = new List<(string, string)>();
}
else
{
valArr = itemStr.Split('=');
if (valArr.Length >= 1)
{
val.Add((valArr[0], valArr[1]));
}
}
}
if (val != null && val.Count > 0)
{
resultDic.Add(groupName, val);
groupName = string.Empty;
val = null;
}
return resultDic;
}
/// <summary>
/// 获取某个分组下的所有键值对
/// </summary>
/// <param name="filePtah">文件路径</param>
/// <param name="groupName">分组名称</param>
/// <returns></returns>
public static List<ValueTuple<string, string>> GetGroupValues(string filePath, string groupName)
{
List<ValueTuple<string, string>> resultList = new List<ValueTuple<string, string>>();
filePath = GetAsbPath(filePath);
byte[] bts = new byte[1024 * 2];
int readNu = GetPrivateProfileSection(groupName, bts, bts.Length, filePath);
string readStr = Encoding.Default.GetString(bts, 0, readNu);
string[] readStrSplicArr = readStr.Split(new string[] { "\0" }, StringSplitOptions.RemoveEmptyEntries);
string[] valArr = null;
foreach (string itemStr in readStrSplicArr)
{
if (itemStr.Contains("="))
{
valArr = itemStr.Split('=');
if (valArr.Length >= 1)
{
resultList.Add((valArr[0], valArr[1]));
}
}
}
return resultList;
}
private static string GetAsbPath(string path)
{
if (!Path.IsPathRooted(path))
{
path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
}
return path;
}
public static bool SetValue(string sectionName, string key, string value, string filePath)
{
bool result;
filePath = GetAsbPath(filePath);
try
{
result = ((int)WritePrivateProfileString(sectionName, key, value, filePath) > 0);
}
catch
{
return false;
}
return result;
}
public static bool RemoveSection(string sectionName, string filePath)
{
bool result;
filePath = GetAsbPath(filePath);
try
{
result = ((int)WritePrivateProfileString(sectionName, null, "", filePath) > 0);
}
catch
{
return false;
}
return result;
}
public static bool Removekey(string sectionName, string key, string filePath)
{
bool result;
filePath = GetAsbPath(filePath);
try
{
result = ((int)WritePrivateProfileString(sectionName, key, null, filePath) > 0);
}
catch
{
return false;
}
return result;
}
}
调用系统C++代码能够很大程度上减少自己去维护 数据结构,应为引用了ValueTuple 要求net框架至少在 4.7 以上。