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);
    }
}

 

你可能感兴趣的:(unity,unity,c#,ini,读取)