C# 怎么获取ini下所有section名称

lidaoying 2020-04-28 02:18:23
C# 怎么获取ini下所有section名称,然后对比textbox1文本内容是否有一样的section名称

比如1.ini内容如下:
[a]
Relay1="系统主正"
Relay2="系统预充"
[b]
Relay1="系统主正"
Relay2="系统预充"
[c]
Relay1="系统主正"
Relay2="系统预充"

.........


........


textbox1 输入a,点击按钮,判断1.ini里面section是不是有a,有就是提示有值,没有就提示没有,
...全文
872 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
muyiliu 2020-04-28
  • 打赏
  • 举报
回复
        /// <summary>         /// 读取指定区域Keys列表。         /// </summary>         /// <param name="Section"></param>         /// <param name="Strings"></param>         /// <returns></returns>         public List<string> ReadSingleSection(string Section, string iniFilename)         {             List<string> result = new List<string>();             byte[] buf = new byte[65536];             uint lenf = GetPrivateProfileString(Section, null, null, buf, (uint)buf.Length, iniFilename);             int j = 0;             for (int i = 0; i < lenf; i++)                 if (buf[i] == 0)                 {                     result.Add(Encoding.Default.GetString(buf, j, i - j));                     j = i + 1;                 }             return result;         } 然后在返回的result中用indexof看看是不是-1,是就不存在。不是就存在
  • 打赏
  • 举报
回复 1

        /// <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("不存在");
            }
        }

萌虎生威 2022-12-31
  • 举报
回复
@大葡萄八块一斤 ReadIniAllSectionName 里面不会内存泄漏吗??

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧