【Unity3D】用C#读取INI配置文件
由于在C#并没有提供专门读取INI文件的API,所以需要间接调用C++的API来进行读取。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; namespace 读写ini文件
{
public class Ini
{
// 声明INI文件的写操作函数 WritePrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // 声明INI文件的读操作函数 GetPrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath); private string sPath = null;
public Ini(string path)
{
this.sPath = path;
} public void Writue(string section, string key, string value)
{ // section=配置节,key=键名,value=键值,path=路径 WritePrivateProfileString(section, key, value, sPath); }
public string ReadValue(string section, string key)
{ // 每次从ini中读取多少字节 System.Text.StringBuilder temp = new System.Text.StringBuilder(); // section=配置节,key=键名,temp=上面,path=路径 GetPrivateProfileString(section, key, "", temp, , sPath); return temp.ToString(); } }
class Program
{
static void Main(string[] args)
{
string Current; Current = Directory.GetCurrentDirectory();//获取当前根目录
Console.WriteLine("Current directory {0}", Current);
// 写入ini
Ini ini=new Ini(Current+"/config.ini");
ini.Writue("Setting","key1","hello word!");
ini.Writue("Setting","key2","hello ini!");
ini.Writue("SettingImg", "Path", "IMG.Path");
// 读取ini
string stemp = ini.ReadValue("Setting","key2");
Console.WriteLine(stemp); Console.ReadKey();
} }
}
想要删除INI中的section的话,可以这样调用:
WritePrivateProfileString(section,null,null,sPath) 想要删除INI中的key的话,可以这样调用:
WritePrivateProfileString(section,key,null,sPath)
【Unity3D】用C#读取INI配置文件的更多相关文章
- bat 读取 ini 配置文件
bat 读取 ini 配置文件 config.ini: abc=abc a=a localpath=D:\local\path .bat: @echo off setlocal enabledelay ...
- 转 python3 读取 ini配置文件
在代码中经常会通过ini文件来配置一些常修改的配置.下面通过一个实例来看下如何写入.读取ini配置文件. 需要的配置文件是: 1 [path] 2 back_dir = /Users/abc/Pych ...
- python读取ini配置文件的示例代码(仅供参考)
这篇文章主要介绍了python读取ini配置文件过程示范,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装 pip install configp ...
- python3笔记-读取ini配置文件
在代码中经常会通过ini文件来配置一些常修改的配置.下面通过一个实例来看下如何写入.读取ini配置文件. 需要的配置文件是: [path] back_dir = /Users/abc/PycharmP ...
- 读取ini配置文件 及 UI对象库
读取ini配置文件 配置项 读取API 写入API 实战:UI 对象库 读取ini配置文件 配置项 在每个 ini 配置文件中,配置数据会被分组(比如下述配置文件中的"config" ...
- Python读取ini配置文件(接口自动测试必备)
前言 大家应该接触过.ini格式的配置文件.配置文件就是把一些配置相关信息提取出去来进行单独管理,如果以后有变动只需改配置文件,无需修改代码. 特别是后续做自动化的测试,代码和数据分享,进行管理.比如 ...
- Python读取ini配置文件的方式
python configparser模块 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- C# 读取Ini配置文件类
配置文件 为fileName.ini 的文件 第一行必须为空,不然读不出值 [section1] key=value key2=value ......... [section2] key=value ...
- 【python3】configparser读取ini配置文件
在应用过程中,发现下面这个问题: cf=configparser.ConfigParser()读取配置文件时,如果数据包含%这们析特殊符号,就会报出上面的错误,使用cf = configparser. ...
随机推荐
- 编译内核是出现:arch/arm/mm/tlb-v4wbi.S:64:error: too many positional arguments
内核:Linux-3.4.2 编译内核出现arch/arm/mm/tlb-v4wbi.S:64:error: too many positional arguments 交叉工具链太老了,换新一点的. ...
- mysql的简单操作
创建数据库并设定字符集: CREATE DATABASE hidb CHARACTER SET ‘utf8’; 使用数据库: use hidb; 删除数据库: DROP DATABASE hidb; ...
- sql server 2008 开启1433端口,开启远程连接
通常情况下只需要设置两处
- nmap 快速扫描所有端口
nmap -sT -sV -Pn -v xxx.xxx.xxx.xxx nmap -sS -p 1-65535 -v 192.168.1.254参数:-sS TCP SYN扫描 nmap ...
- 分页sql汇总
1.oracle数据库分页 select * from (select a.*,rownum rc from 表名 where rownum<=endrow) a where a.rc>= ...
- spring+jax 出现java.io.Serializable is an interface, and JAXB can't handle interfaces
spring+jax 出现java.io.Serializable is an interface, and JAXB can't handle interfaces 原因是我的webservice方 ...
- 各版本Google浏览器下载地址
各版本谷歌浏览器下载地址 https://www.chromedownloads.net/chrome64win/
- ARC097E Sorted and Sorted
传送门 题目 There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are ...
- p4503&bzoj3555 企鹅QQ
传送门(洛谷) 传送门(bzoj) 题目 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相 ...
- 图像特征--HOG特征
1.HOG特征:方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和统计图像局部区域的梯 ...