1、ini文件简介
主要用于存放数据量较小的文本性质的配置信息,具体实施根据实际需求来定。
1.1 文件格式
[SectionName1] //节点1
KeyName1=Value1 //key/value
KeyName2=Value2
........
[SectionName2] //节点2
KeyName1=Value1
KeyName2=Value2
........
1.2 文件内容示意
例如:xiaoming.ini
;注解下本文档仅起举例示意作用
[Character]
Height=180
Long=60
Wide=50
Circle=5
ContinueTime=forever
Degree=doctor
Profession=ThePresident
[Hobby]
InterstellerTravel=frequently
Airship=99+
[Hate]
Species=NoHave
2、Unity(c#)读写ini的使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;
using System.Text;
/// <summary>
/// ini类文件读写辅助类
/// </summary>
public class FileHelper
{
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string defValue,StringBuilder recValue,int size,string filePath);//读取信息函数
[DllImport("kernel32")]
private static extern int WritePrivateProfileString(string section, string key, string value,string filePath);//写入信息函数
private string filePath = "";//ini文件路径
public FileHelper(string filepath)
{
filePath = Application.streamingAssetsPath + "/" + filepath + ".ini";
}
/// <summary>
/// 写入操作
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public void Write(string section, string key, string value)
{
try
{
WritePrivateProfileString(section,key,value,filePath);
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
/// <summary>
/// 读取ini文件内容
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <returns></returns>
public string Read(string section, string key)
{
try
{
StringBuilder tmpSB = new StringBuilder(255);
GetPrivateProfileString(section,key,"",tmpSB,255,filePath);
return tmpSB.ToString();
}
catch (Exception e)
{
Debug.Log(e.Message);
return "";
}
}
public static string configPath = Application.streamingAssetsPath + "/xiaoming.ini";
public static void IniWrite(string section, string key, string value, string path)
{
WritePrivateProfileString(section,key,value,path);
}
public static string IniRead(string section, string key, string path)
{
StringBuilder tmpSB = new StringBuilder(255);
GetPrivateProfileString(section, key, "", tmpSB, 255, path);
return tmpSB.ToString();
}
}