C# 读取ini配置文件

参考文章:

  1. https://blog.csdn.net/luthreestone/article/details/78119647
  2. https://blog.csdn.net/weixin_44713908/article/details/110134975
class IniFileUtil
{
    public string m_sIniFile = "";             //INI文件名  

    [DllImport("kernel32.dll")]
    private extern static int WritePrivateProfileSection(string section, string value, string fileName);

    [DllImport("kernel32.dll")]
    private static extern int WritePrivateProfileString(string section, string keyName, string value, string fileName);

    [DllImport("kernel32.dll")]
    private static extern int GetPrivateProfileString(string section, string keyName, string def, StringBuilder buffer, int size, string fileName);

    [DllImport("kernel32.dll")]
    private extern static int GetPrivateProfileString(string section, string keyName, string def, byte[] buffer, int size, string fileName);

    [DllImport("kernel32.dll")]
    private extern static int GetPrivateProfileSectionNames(byte[] buffer, int size, string fileName);

    [DllImport("kernel32.dll")]
    private static extern int GetPrivateProfileSection(string section, byte[] buffer, int size, string fileName);

    [DllImport("kernel32.dll")]
    private extern static int GetPrivateProfileSection(string section, StringBuilder buffer, int size, string fileName);

    //声明读写INI文件的API函数  
    public IniFileUtil(string sIniFile)
    {
        m_sIniFile = sIniFile;
    }

    //类的构造函数,传递INI文件名  
    public void IniWriteValue(string Section, string Key, string Value)
    {
        WritePrivateProfileString(Section, Key, Value, m_sIniFile);
    }

    //读INI文件  
    public string IniGetValue(string Section, string Key)
    {
        StringBuilder temp = new StringBuilder(255);
        GetPrivateProfileString(Section, Key, "", temp, 255, m_sIniFile);
        return temp.ToString();
    }

    // 封装的方法中,最有价值的是获取所有Sections和所有的Keys,网上关于这个的代码大部分是错误的,这里给出一个正确的方法:  
    // 返回该配置文件中所有Section名称的集合  
    public ArrayList GetSections()
    {
        byte[] buffer = new byte[65535];
        int rel = GetPrivateProfileSectionNames(buffer, buffer.GetUpperBound(0), m_sIniFile);
        int iCnt, iPos;
        ArrayList arrayList = new ArrayList();
        string tmp;
        if (rel > 0)
        {
            iCnt = 0; iPos = 0;
            for (iCnt = 0; iCnt < rel; iCnt++)
            {
                if (buffer[iCnt] == 0x00)
                {
                    tmp = System.Text.ASCIIEncoding.UTF8.GetString(buffer, iPos, iCnt - iPos).Trim();
                    iPos = iCnt + 1;
                    if (tmp != "")
                        arrayList.Add(tmp);
                }
            }
        }
        return arrayList;
    }

    // 获取节点的所有KEY值  
    public ArrayList GetKeys(string SectionName)
    {
        byte[] buffer = new byte[5120];
        int rel = GetPrivateProfileString(SectionName, null, "", buffer, buffer.GetUpperBound(0), m_sIniFile);  

        int iCnt, iPos;
        ArrayList arrayList = new ArrayList();
        string tmp;
        if (rel > 0)
        {
            iCnt = 0; iPos = 0;
            for (iCnt = 0; iCnt < rel; iCnt++)
            {
                if (buffer[iCnt] == 0x00)
                {
                    tmp = System.Text.ASCIIEncoding.UTF8.GetString(buffer, iPos, iCnt - iPos).Trim();
                    iPos = iCnt + 1;
                    if (tmp != "")
                        arrayList.Add(tmp);
                }
            }
        }
        return arrayList;
    }

    public Dictionary<string, string> GetKeysAndValues(string category)
    {
        byte[] buffer = new byte[2048];

        GetPrivateProfileSection(category, buffer, 2048, m_sIniFile);
        String[] tmp = System.Text.ASCIIEncoding.UTF8.GetString(buffer).Trim('\0').Split('\0');
        Dictionary<string, string> result = new Dictionary<string, string>();
        foreach (String entry in tmp)
        {
            if(entry[0] == '#')
            {
                continue;
            }

            string[] v = entry.Split('=');
            result.Add(v[0], v[1]);
        }
        return result;
    }
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值