c#怎么读写.ini配置文件?下面itmemo小编给大家分享一段(c)Sharp读写配置文件代码。
/// <summary>
/// 读写配置项文件*.ini
/// </summary>
public class ReadAndWriteIni
{
// 声明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);
public static bool Write(string section, string key, string value, string sPath)
{
// section=配置节,key=键名,value=键值,path=路径
long bOK = WritePrivateProfileString(section, key, value, sPath);
return true;
}
public static string Read(string section, string key, string defaultVal, string sPath)
{
// 每次从ini中读取多少字节
System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
// section=配置节,key=键名,defaultVal=当没有读到值时返回模式值,temp=上面,path=路径;
GetPrivateProfileString(section, key, defaultVal, temp, 255, sPath);
return temp.ToString();
}
}
直接复制以上代码新建类调用即可,一般数据量不大的文件都用.ini读写配置项,所以这个还是比较实用的!
正在阅读:
c#读写.ini配置文件源码05-29
2019贵州黔西南州义龙新区事业单位招聘医疗岗公告【29人】11-01
圣诞节作文400字:假如我是圣诞老人06-20
2018年宁夏中卫考研报名网址:http://yz.chsi.com.cn/01-23
德国留学如何勤工俭学02-12
劳动教育作文800字议论文高中:高中关于教育的议论文800字05-11
一只鸟儿相遇作文700字07-08
总结报告个人工作幼儿园10篇07-14