欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#实现ini文件读写类分享

程序员文章站 2024-02-24 12:19:48
复制代码 代码如下:///     /// 读写ini文件的类。    /// &...

复制代码 代码如下:

/// <summary>
    /// 读写ini文件的类。
    /// </summary>
    public class inihelper
    {
        // 读写ini文件相关。
        [dllimport("kernel32.dll", entrypoint = "writeprivateprofilestring", charset = charset.ansi)]
        public static extern long writeprivateprofilestring(string section, string key, string val, string filepath);
        [dllimport("kernel32.dll", entrypoint = "getprivateprofilestring", charset = charset.ansi)]
        public static extern int getprivateprofilestring(string section, string key, string def, stringbuilder retval, int size, string filepath);

        [dllimport("kernel32.dll", entrypoint = "getprivateprofilesectionnames", charset = charset.ansi)]
        public static extern int getprivateprofilesectionnames(intptr lpszreturnbuffer, int nsize, string filepath);

        [dllimport("kernel32.dll ", entrypoint = "getprivateprofilesection", charset = charset.ansi)]
        public static extern int getprivateprofilesection(string lpappname, byte[] lpreturnedstring, int nsize, string filepath);


        /// <summary>
        /// 向ini写入数据。
        /// </summary>
        /// <param name="section">节点名。</param>
        /// <param name="key">键名。</param>
        /// <param name="value">值名。</param>
        public static void write(string section, string key, string value, string path)
        {
            writeprivateprofilestring(section, key, value, path);
        }


        /// <summary>
        /// 读取ini数据。
        /// </summary>
        /// <param name="section">节点名。</param>
        /// <param name="key">键名。</param>
        /// <param name="path">值名。</param>
        /// <returns>相应的值。</returns>
        public static string read(string section, string key, string path)
        {
            stringbuilder temp = new stringbuilder(255);
            int i = getprivateprofilestring(section, key, "", temp, 255, path);
            return temp.tostring();
        }

        /// <summary>
        /// 读取一个ini里面所有的节
        /// </summary>
        /// <param name="sections"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static int getallsectionnames(out string[] sections, string path)
        {
            int max_buffer = 32767;
            intptr preturnedstring = marshal.alloccotaskmem(max_buffer);
            int bytesreturned = getprivateprofilesectionnames(preturnedstring, max_buffer, path);
            if (bytesreturned == 0)
            {
                sections = null;
                return -1;
            }
            string local = marshal.ptrtostringansi(preturnedstring, (int)bytesreturned).tostring();
            marshal.freecotaskmem(preturnedstring);
            //use of substring below removes terminating null for split
            sections = local.substring(0, local.length - 1).split('\0');
            return 0;
        }

        /// <summary>
        /// 得到某个节点下面所有的key和value组合
        /// </summary>
        /// <param name="section"></param>
        /// <param name="keys"></param>
        /// <param name="values"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static int getallkeyvalues(string section, out string[] keys, out string[] values, string path)
        {
            byte[] b = new byte[65535];

            getprivateprofilesection(section, b, b.length, path);
            string s = system.text.encoding.default.getstring(b);
            string[] tmp = s.split((char)0);
            arraylist result = new arraylist();
            foreach (string r in tmp)
            {
                if (r != string.empty)
                    result.add(r);
            }
            keys = new string[result.count];
            values = new string[result.count];
            for (int i = 0; i < result.count; i++)
            {
                string[] item = result[i].tostring().split(new char[] { '=' });
                if (item.length == 2)
                {
                    keys[i] = item[0].trim();
                    values[i] = item[1].trim();
                }
                else if (item.length == 1)
                {
                    keys[i] = item[0].trim();
                    values[i] = "";
                }
                else if (item.length == 0)
                {
                    keys[i] = "";
                    values[i] = "";
                }
            }

            return 0;
        }

    }