/// <summary> /// 获取INI文件中指定节点(Section)中的所有条目(key=value形式) /// </summary> /// <param name="section">节点名称</param> /// <returns>指定节点中的所有项目,没有内容返回string[0]</returns> public static string[] ReadAllItems(string iniPath, string section) { //返回值形式为 key=value,例如 Color=Red uint MAX_BUFFER = 32767; //默认为32767 string[] items = new string[0]; //返回值 //分配内存 IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = IniFileHelper.GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniPath); if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0)) { string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned); // v1.2 modify 2016-10-21 support # remark. items = (from item in returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries) where item.StartsWith("#") == false select item).ToArray(); } Marshal.FreeCoTaskMem(pReturnedString); //释放内存 return(items); }