通过C#读取ini文件中的内容

来源:互联网 发布:逆战ak12淘宝多少钱 编辑:程序博客网 时间:2024/06/07 02:08
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;


namespace CommFunc
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> listSection =
                IniFunc.ReadSections("D:\\OMNeT++\\samples\\tokenring\\omnetpp.ini");
            //Console.WriteLine("下面是omnetpp.ini文件的所有的段");
            for (int i = 0; i < listSection.Count; i++)
            {
                Console.WriteLine(listSection[i]);
                List<string> listKey = IniFunc.ReadKeyValues(listSection[i], "D:\\OMNeT++\\samples\\tokenring\\omnetpp.ini");
               
                Console.WriteLine("");
                for (int j = 0; j < listKey.Count; j++)
                {
                    Console.WriteLine(listKey[j]);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            


        }
    }




    public class IniFunc
    {
        [DllImport("kernel32")]
        public extern static int GetPrivateProfileSectionNames(byte[] buffer, int iLen,
            string fileName);


        [DllImport("kernel32")]
        public extern static int GetPrivateProfileSection(string section, byte[] buffer, int nSize, string filePath);


        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(
        string section,string key,string defVal,StringBuilder retVal,int size,string filePath);


        [DllImport("kernel32")]
        private static extern int GetPrivateProfileInt(
        string lpAppName,string lpKeyName,int nDefault,string lpFileName);






       
        //获取段名
        public static List<string> ReadSections(string filePath)
        {
            byte[] buffer=new byte[65535];
            int rel=GetPrivateProfileSectionNames(buffer,buffer.GetUpperBound(0),filePath);
            int iCnt,iPos;
            List<string> arrayList=new List<string>();
            string tmp;
            if(rel>0)
            {
                iCnt=0;iPos=0;
                for(iCnt=0;iCnt<rel;iCnt++)
                {
                    if(buffer[iCnt]==0x00)
                    {
                        tmp=System.Text.ASCIIEncoding.Default.GetString(buffer,iPos,iCnt-iPos).Trim();
                        iPos=iCnt+1;
                        if(tmp!="")
                        { arrayList.Add(tmp);}


                    }
                }
            }


            return arrayList;
        }




        //获取指定段名下的所有参数
        public static List<string> ReadKeyValues(string section,string filePath)
        {
            byte[] buffer =new byte[32767];
            List<string> list=new List<string>();
            int length=GetPrivateProfileSection(section,buffer,buffer.GetUpperBound(0),filePath);
            string temp;
            int postion=0;
            for (int i=0;i<length;i++)
            {
                if(buffer[i]==0x00)
                {
                temp=System.Text.ASCIIEncoding.Default.GetString(buffer,postion,i-postion).Trim();
                postion=i+1;
                if(temp.Length>0)
                {list.Add(temp);}
            }
        }
        return list;
        }




    }


}
原创粉丝点击