C# 读写INI的类
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace WriteReadIni
{
/// <summary>
/// WriteReadIni 的摘要说明:对ini文件进行读写的类,调用API函数实现。
///
/// 写操作函数WritePrivateProfileString()
/// WritePrivateProfileString(string section,string key,string val,string filePath);
/// 参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。
///
/// 读操作函数GetPrivateProfileString()
/// GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int Size,string filePath);
/// 参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。
/// </summary>
public class WriteReadIni
{
[DllImport("kernel32")]
private static extern int WritePrivateProfileString (string section,string key,string val,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString (string section,string key,string def,StringBuilder retVal,int Size,string filePath);
public WriteReadIni()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void WriteIni(string section,string key,string val,string filePath)
{
WritePrivateProfileString(section,key,val,filePath);
}
public string ReadIni(string section,string key,string filePath)
{
StringBuilder temp = new StringBuilder(500);
GetPrivateProfileString (section,key,"",temp,500,filePath);
return temp.ToString();
}
}
}
using System.Runtime.InteropServices;
using System.Text;
namespace WriteReadIni
{
/// <summary>
/// WriteReadIni 的摘要说明:对ini文件进行读写的类,调用API函数实现。
///
/// 写操作函数WritePrivateProfileString()
/// WritePrivateProfileString(string section,string key,string val,string filePath);
/// 参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。
///
/// 读操作函数GetPrivateProfileString()
/// GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int Size,string filePath);
/// 参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。
/// </summary>
public class WriteReadIni
{
[DllImport("kernel32")]
private static extern int WritePrivateProfileString (string section,string key,string val,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString (string section,string key,string def,StringBuilder retVal,int Size,string filePath);
public WriteReadIni()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void WriteIni(string section,string key,string val,string filePath)
{
WritePrivateProfileString(section,key,val,filePath);
}
public string ReadIni(string section,string key,string filePath)
{
StringBuilder temp = new StringBuilder(500);
GetPrivateProfileString (section,key,"",temp,500,filePath);
return temp.ToString();
}
}
}