110,534
社区成员
发帖
与我相关
我的任务
分享
/// <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);
/// <summary>
/// 获取指定ini文件中所有节点名称
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
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 = textbox1.Text;
string[] sections = ReadIniAllSectionName(@"ini文件路径");
if (sections.Contains(str))
{
Console.WriteLine("存在");
}
else
{
Console.WriteLine("不存在");
}
}