Ini操作类

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

public class INIFileUtil
{
    /// <summary>
    /// INI文件地址
    /// </summary>
    private string path;

    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="INIPath">路径</param>
    private INIFileUtil(string INIPath)
    {
        this.path = INIPath;
    }

    public static INIFileUtil LoadIniFile(string iniFile)
    {
        return new INIFileUtil(iniFile);
    }
    /// <summary>
    /// 构造函数
    /// </summary>
    private INIFileUtil()
    {
    }

    [DllImport("kernel32", EntryPoint = "WritePrivateProfileString")]
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
    [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
    private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
    [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSectionNames", CharSet = CharSet.Ansi)]
    private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, int nSize, string filePath);
    [DllImport("KERNEL32.DLL ", EntryPoint = "GetPrivateProfileSection", CharSet = CharSet.Ansi)]
    private 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 void IniWriteValue(string Section, string Key, string Value)
    {
        INIFileUtil.WritePrivateProfileString(Section, Key, Value, this.path);
    }

    /// <summary>
    ///  读取INI文件
    /// </summary>
    /// <param name="Section">分组节点</param>
    /// <param name="Key">关键字</param>
    /// <returns></returns>
    public string IniReadValue(string Section, string Key)
    {
        StringBuilder stringBuilder = new StringBuilder(255);
        INIFileUtil.GetPrivateProfileString(Section, Key, "", stringBuilder, 255, this.path);
        return stringBuilder.ToString();
    }

    /// <summary>
    /// 读取INI文件
    /// </summary>
    /// <param name="section">分组节点</param>
    /// <param name="key">关键字</param>
    /// <returns></returns>
    public byte[] IniReadValues(string section, string key)
    {
        byte[] array = new byte[255];
        INIFileUtil.GetPrivateProfileString(section, key, "", array, 255, this.path);
        return array;
    }

    /// <summary>
    /// 删除ini文件下所有段落
    /// </summary>
    public void ClearAllSection()
    {
        this.IniWriteValue(null, null, null);
    }

    /// <summary>
    /// 删除ini文件下指定段落下的所有键
    /// </summary>
    /// <param name="Section">指定段</param>
    public void ClearSection(string Section)
    {
        this.IniWriteValue(Section, null, null);
    }

    /// <summary>
    /// 读取一个ini里面所有的节
    /// </summary>
    /// <param name="sections"></param>
    /// <param name="path"></param>
    /// <returns></returns>
    public int GetAllSectionNames(out List<string> sections)
    {
        int MAX_BUFFER = 32767;
        IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER);
        int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path);
        if (bytesReturned == 0)
        {
            sections = new List<string>();
            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('').ToList<string>();
        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 int GetAllKeyValues(string section, out string[] keys, out string[] values)
    {
        byte[] b = new byte[65535];

        GetPrivateProfileSection(section, b, b.Length, this.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;
    }
}
原文地址:https://www.cnblogs.com/anbylau2130/p/3483170.html