//首先引入Win32 API
/// <summary>
/// 获取所有节点名称
/// </summary>
/// <param name="lpszReturnBuffer">存放节点名称的内存地址,每个节点之间用\0分隔</param>
/// <param name="nSize">内存大小(characters)</param>
/// <param name="filePath"></param>
/// <returns></returns>
[DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string filePath);
//查找ini文件中的所有节点名
public static string[] ReadIniAllSectionName(string filePath)
{
uint MaxBuffer = 32767;
string[] sections = new string[0]; // 返回值
// 申请内存
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MaxBuffer * sizeof(char));
uint byteReturned = GetPrivateProfileSectionNames(pReturnedString, MaxBuffer, filePath);
if (byteReturned != 0)
{
// 读取指定内存内容
string local = Marshal.PtrToStringAuto(pReturnedString, (int)byteReturned).ToString();
// 每个节点之间用\0分隔,末尾有一个\0
sections = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
}
return sections;
}
//查找某一个节点是否在该文件中
public static void Test()
{
string str = "测试1";
string[] sections = ReadIniAllSectionName(@"C:\Users\Admin\Desktop\测试文件.ini");
if (sections.Contains(str))
{
Console.WriteLine("存在");
}
else
{
Console.WriteLine("不存在");
}
}
参考文章:
//引入Win32 API
// Windows API 参考手册:http://www.office-cn.net/t/api/index.html?writeprivateprofilestring.htm
// https://learn.microsoft.com/zh-cn/windows/win32/api/winbase/nf-winbase-writeprivateprofilestringa
/// <summary>
///
/// </summary>
/// <param name="lpAppName">节的名称。如果节不存在,则会创建一个。</param>
/// <param name="lpKeyName">键的名称。如果指定节中不存在该键,则会创建它。如果此参数为null,则会删除整个节(包括该节中的所有条目)。</param>
/// <param name="lpString">要写入文件的字符串。如果此参数为null,则删除lpKeyName参数指向的键。</param>
/// <param name="lpFileName">初始化文件的名称。</param>
/// <returns>如果函数成功,则返回值为非零。如果函数失败,则返回值为零。</returns>
[DllImport("kernel32.dll")]
private static extern int WritePrivateProfileStringA(string lpAppName, string lpKeyName, string lpString, string lpFileName);
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprivateprofilestring
/// <summary>
///
/// </summary>
/// <param name="lpAppName">节的名称。如果此参数为null,则会将文件中所有节的名称复制到提供的缓冲区中。</param>
/// <param name="lpKeyName">键的名称。如果此参数为null,则会将lpAppName中所有键的名称复制到提供的缓冲区中。</param>
/// <param name="lpDefault">默认字符串。如果在初始化文件中找不到lpKeyName,则会将默认字符串复制到提供的缓冲区中。如果此参数为null,则默认为空字符串""。</param>
/// <param name="lpReturnedString">缓冲区。</param>
/// <param name="nSize">缓冲区的大小,以字符为单位。</param>
/// <param name="lpFileName">初始化文件的名称。</param>
/// <returns>返回值是复制到缓冲区的字符数。</returns>
[DllImport("kernel32.dll")]
private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
[DllImport("kernel32.dll")]
private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, byte[] lpReturnedString, int nSize, string lpFileName);
//***********************************封装方法********************************************
/// <summary>
/// 读值。
/// </summary>
/// <param name="filePath">ini文件的路径。示例:H:\学习\C#\示例\Demo.ini</param>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <returns>值</returns>
public static string ReadValue(string filePath, string section, string key)
{
StringBuilder sb = new StringBuilder(1024);
var charLength = GetPrivateProfileString(section, key, "", sb, sb.Capacity, filePath);
return sb.ToString();
}
/// <summary>
/// 写值。
/// </summary>
/// <param name="filePath">ini文件的路径。示例:H:\学习\C#\示例\Demo.ini</param>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns>true表示写入成功,false表示写入失败。</returns>
public static bool Write(string filePath, string section, string key, string value)
{
int val = WritePrivateProfileStringA(section, key, value, filePath);
if (val != 0)
return true;
else
return false;
}
/// <summary>
/// 删除指定的键。
/// </summary>
/// <param name="filePath">ini文件的路径。示例:H:\学习\C#\示例\Demo.ini</param>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <returns>true表示删除成功,false表示删除失败。</returns>
public static bool DeleteKey(string filePath, string section, string key)
{
int val = WritePrivateProfileStringA(section, key, null, filePath);
if (val != 0)
return true;
else
return false;
}
/// <summary>
/// 删除指定的节。
/// </summary>
/// <param name="filePath">ini文件的路径。示例:H:\学习\C#\示例\Demo.ini</param>
/// <param name="section">节</param>
/// <returns>true表示删除成功,false表示删除失败。</returns>
public static bool DeleteSection(string filePath, string section)
{
var val = WritePrivateProfileStringA(section, null, null, filePath);
if (val != 0)
return true;
else
return false;
}
/// <summary>
/// 读出指定节中所有的键。
/// </summary>
/// <param name="filePath">ini文件的路径。示例:H:\学习\C#\示例\Demo.ini</param>
/// <param name="section">节</param>
/// <returns>指定节中所有的键。</returns>
public static List<string> ReadKey(string filePath, string section)
{
List<string> keys = new List<string>();
byte[] buf = new byte[2000];
var charLength = GetPrivateProfileString(section, null, "", buf, buf.Length, filePath);
int j = 0;
for (int i = 0; i < charLength; i++)
{
if (buf[i] == 0)
{
keys.Add(Encoding.Default.GetString(buf, j, i - j));
j = i + 1;
}
}
return keys;
}
/// <summary>
/// 读出文件中所有的节。
/// </summary>
/// <param name="filePath">ini文件的路径。示例:H:\学习\C#\示例\Demo.ini</param>
/// <returns>文件中所有的节。</returns>
public static List<string> ReadSection(string filePath)
{
List<string> keys = new List<string>();
byte[] buf = new byte[2000];
var charLength = GetPrivateProfileString(null, null, "", buf, buf.Length, filePath);
int j = 0;
for (int i = 0; i < charLength; i++)
{
if (buf[i] == 0)
{
keys.Add(Encoding.Default.GetString(buf, j, i - j));
j = i + 1;
}
}
return keys;
}
//***********************************按钮点击测试**************************************
private void button5_Click(object sender, EventArgs e)
{
string str = ReadValue(@"C:\Users\Admin\Desktop\测试文件.ini", "测试1", "第一");//读键值
bool bo = Write(@"C:\Users\Admin\Desktop\测试文件.ini", "测试1", "第一", "AA");//写键值
bo = DeleteKey(@"C:\Users\Admin\Desktop\测试文件.ini", "测试1", "第一");//删除键
bo = DeleteSection(@"C:\Users\Admin\Desktop\测试文件.ini", "测试1");//删除节
List<string> aa = ReadKey(@"C:\Users\Admin\Desktop\测试文件.ini", "测试1");//读某节点下所有键名
aa = ReadSection(@"C:\Users\Admin\Desktop\测试文件.ini");//读ini文件下所有节点名
}
参考文章: