unityでのC#iniドキュメントからの情報の読み出し


メモ:iniドキュメントからの情報の読み取り(Androidデバイスでは、読み取りに失敗しました.Androidデバイスでは「\」を「//」に変更してもダメです)
 
// ini       
//  : config.ini     ,     (100,200)
//    :
//[para]
//time = 100
//   = 200
//[var]
//num = 5

using UnityEngine;
using System;

public class readInfroINITest : MonoBehaviour
{
    ConfigIni iniObj;

    // Start is called before the first frame update
    void Start()
    {
        string pathTemp = "";
        if (Application.platform == RuntimePlatform.Android)
        {
            pathTemp = "/storage/emulated/0/DCIM";   //Android    
        }
        else
        {
            pathTemp = Application.streamingAssetsPath; //streaming    
            //pathTemp = "D:/storage/emulated/0/DCIM";   //      
            //System.Environment.CurrentDirectory //    
        }

        string configStr = pathTemp + "\\" + "config.ini";

        iniObj = new ConfigIni(configStr);

        Debug.Log(readString("para", "time"));
        Debug.Log(readString("para", "  "));
        Debug.Log(readString("var", "num"));

        Debug.Log(10 - readInt("para", "time"));
        Debug.Log(10 - readInt("para", "  "));
        Debug.Log(10 - readInt("var", "num"));

    }

    /// 
    /// string  
    /// 
    /// 
    /// 
    /// 
    string readString(string part, string key)
    {
        return iniObj.ReadIniContent(part, key);
    }

    /// 
    /// int 
    /// 
    /// 
    /// 
    /// 
    int readInt(string part, string key)
    {
        try
        {//             
            string readValue = iniObj.ReadIniContent(part, key);

            return Convert.ToInt32(readValue);
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
            return 0;
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;

/// 
///   ini    
/// [Time] 
/// time=10 
/// [Speed] 
/// speed=5 
/// ConfigIni ini=new ConfigIni(Application.StreamingAssets+"/Setting.ini"); 
/// time=ini.ReadIniContent("Time","time");
/// speed=ini.ReadIniContent("Speed","speed");
/// ini.WritePrivateProfileString("Count","count","5");
/// 
public class ConfigIni
{

    public string path;
    public Dictionary keyVal = new Dictionary();

    //ini       
    public ConfigIni(string path)
    {
        this.path = path;

        StreamReader sr = new StreamReader(path, Encoding.Default);
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            if (line.Contains("="))
            {
                string[] kv = line.Split('=');
                string key = kv[0].Trim();
                string v = kv[1].Trim();
                keyVal.Add(key, v);
            }
        }
    }

    [DllImport("kernel32")]
    public static extern long WritePrivateProfileString(string section, string key, string value, string path);
    [DllImport("kernel32")]
    public static extern int GetPrivateProfileString(string section, string key, string deval, StringBuilder stringBuilder, int size, string path);
    [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern int MessageBox(IntPtr handle, String message, String title, int type);

    //  ini  
    public void WriteIniContent(string section, string key, string value)
    {
        WritePrivateProfileString(section, key, value, this.path);
    }

    //  Ini  
    public string ReadIniContent(string section, string key)
    {
        StringBuilder temp = new StringBuilder(255);
        int i = GetPrivateProfileString(section, key, "", temp, 255, this.path);
        //MessageBox(IntPtr.Zero, this.path+i + ","+temp+","+section+key, "ReadIniContent", 0);
        return temp.ToString();
    }
    //        
    public bool IsIniPath()
    {
        return File.Exists(this.path);
    }
}