/// <summary> /// 读取INI文件中指定INI文件中的所有节点名称(Section) /// </summary> /// <param name="iniFile">Ini文件</param> /// <returns>所有节点,没有内容返回string[0]</returns> public static string[] INIGetAllSectionNames(string iniFile) { uint MAX_BUFFER = 32767; //默认为32767 string[] sections = new string[0]; //返回值 //申请内存 IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = INIOperationHelper.GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile); if (bytesReturned != 0) { //读取指定内存的内容 string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString(); //每个节点之间用\0分隔,末尾有一个\0 sections = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } //释放内存 Marshal.FreeCoTaskMem(pReturnedString); return(sections); }