ini (ini-parser)配置文件解析 for donet

本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhaoqingqing/p/6593538.html



介绍#

此ini解析库适用于mono(unity3d),donet,大小在30kb左右。

开源免费:https://github.com/rickyah/ini-parser

 

使用示例#

engine_config.ini  配置文件内容如下

复制代码
[Engine]
: product real path
ProductRelPath    = ../Product    
AssetBundleBuildRelPath    = ../Product/Bundles    
StreamingBundlesFolderName = Bundles    
AssetBundleExt = .bytes    
IsLoadAssetBundle = 1
复制代码

使用方法如下

复制代码
using System;
using IniParser.Model;
using IniParser.Model.Formatting;
using IniParser.Parser;

public class EngineConfigs
{
    private readonly IniData _iniData;

    public EngineConfigs(string iniconfig)
    {
        var parser = new IniDataParser();
        _iniData = parser.Parse(iniconfig);
    }

    /// <summary>
    /// GetConfig from section
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <param name="throwError">whether or not throw error when get no config</param>
    /// <returns></returns>
    public string GetConfig(string section, string key, bool throwError = true)
    {
        var sectionData = _iniData[section];
        if (sectionData == null)
        {
            if (throwError)
                throw new Exception("Not found section from ini config: " + section);
            return null;
        }
        var value = sectionData[key];
        if (value == null)
        {
            if (throwError)
                throw new Exception(string.Format("Not found section:`{0}`, key:`{1}` config", section, key));
        }
        return value;
    }
}


public class IniParseDemo
{
    public void Main()
    {
        EngineConfigs engineConfigs = new EngineConfigs("xxx");
        //此处返回 ../Product    
        var productRelPath = engineConfigs.GetConfig("Engine", "ProductRelPath");
    }
}
复制代码

注意事项

IniDataParser.Parse(iniconfig);

iniconfig是具体的内容,而不是某个ini文件

 

其它

示例:https://github.com/zhaoqingqing/blog_samplecode/tree/master/unity_helper/iniparser

或者参照 KEngine中的AppEngine使用:https://github.com/mr-kelly/KEngine/blob/3351991a9eb7593a53a6070eddff5dd912fc6d7a/KEngine.UnityProject/Assets/KEngine/AppEngine.cs

作者:赵青青   一名在【网易游戏】做游戏开发的程序员,擅长Unity3D,游戏开发,.NET等领域。
本文版权归作者和博客园共有,欢迎转载,转载之后请务必在文章明显位置标出原文链接和作者,谢谢。
如果本文对您有帮助,请点击【推荐】您的赞赏将鼓励我继续创作!想跟我一起进步么?那就【关注】我吧。
posted @   赵青青  阅读(1742)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 易语言 —— 开山篇
· Trae初体验
CONTENTS
点击右上角即可分享
微信分享提示