引用“kernel32”读写ini配置文件

引用“kernel32”读写ini配置文件

引用“kernel32”读写ini配置文件

OverView

kernel32.dll是Windows中非常重要的32位动态链接库文件,属于内核级文件。它控制着系统的内存管理、数据的输入输出操作和中断处理,当Windows启动时,kernel32.dll就驻留在内存中特定的写保护区域,使别的程序无法占用这个内存区域。
standardshader与toonshader比较:
![standardshader][1]![toonshader][2]

注意事项:

  • ini文件路径必须完整
  • ini文件路径必须使用\,因为VC++中,\\才表示\
  • 可将ini放在程序所在目录,此时IpFileName参数为“.\FileName.ini”

GetPrivateProfileInt

使用该方法可获取ini类型数据,未获取到时则会取设置的默认数据

UINT WINAPI GetPrivateProfileInt
(
_In_LPCTSTR lpAppName, //ini文件中区块名称
_In_LPCTSTR lpKeyName, //键名
_In_INT nDefault, //默认值
_In_LPCTSTR lpFileName //ini文件路径
);

GetPrivateProfileString

使用该方法可获取string类型数据,未获取到时则会取设置的默认数据

UINT WINAPI GetPrivateProfileString
(
_In_LPCTSTR lpAppName, //ini文件中区块名称
_In_LPCTSTR lpKeyName, //键名
_In_INT nDefault, //默认值
_In_LPSTR lpReturnedString,//接受ini文件中值的CString对象,指定一个字符串缓冲区,长度至少为nSize
_In_DWORD nSize,//指定装载到IpReturnedString缓冲区的最大字符数
_In_LPCTSTR lpFileName //ini文件路径
);

向ini中写值,所以仅有写入string就足够了

WritePrivateProfileString

BOOL WritePrivateProfileString(
LPCTSTR lpAppName,//ini文件中区块名
LPCTSTR lpKeyName,//键名
LPCTSTR lpString,//键值
LPCTSTR lpFileName
);

unity中简单使用

Singleton

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;

/// <summary>
/// ws:配置文件读写类,这就很nice。配置文件必须放在StreamingAssets文件夹下
/// </summary>
public class ConfigUtilty : Singleton<ConfigUtilty> {

    // StringBuilder temp = new StringBuilder(1024);
    //int i = GetPrivateProfileString("system", "StrServeIP", "", temp, 500, g_ConfigIniFile);
    //SetProfileString("Flag","Flag","10","MainConfig.ini");

    //系统动态库
    [DllImport("kernel32")]
    private static extern void WritePrivateProfileString(string section, string key, string def,string filePath);
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileInt(string section, string key, int def, string filePath);
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

     private string ConfigPath;//配置文件所在位置
     private StringBuilder retVal;
     void Awake()
     {
         ConfigPath = Application.streamingAssetsPath;
         retVal = new StringBuilder();

    }

    
    public void SetProfileString(string section,string key,string def,string filename)
    {
        WritePrivateProfileString(section,key,def,ConfigPath+"\\"+filename);
    }

    public int GetProfileInt(string section,string key,int def,string filename)
    {
        return GetPrivateProfileInt(section, key, def, this.ConfigPath+"\\"+filename);
    }

    public string GetProfileString(string section,string key,string def,int size,string filename)
    {
        int i=GetPrivateProfileString(section, key,def,retVal,size,ConfigPath + "\\" + filename);
        return i == 0 ? "" : retVal.ToString();
    }


}

猜你喜欢

转载自www.cnblogs.com/Firepad-magic/p/9005003.html