using system;
using system.io;
using system.runtime.interopservices;
using system.text;
using microsoft.win32;
namespace wjb.readorwriteiniandreg
{
/// <summary>
/// rwini 的摘要说明。
/// 读写ini文件类
/// 类库开发:吴剑冰
/// 时间:2003年10月20日
/// 功能:读写ini文件
/// </summary>
public class rwini
{
private static string filename;
[dllimport("kernel32")]
private static extern int getprivateprofileint(
string lpappname,
string lpkeyname,
int ndefault,
string lpfilename
);
[dllimport("kernel32")]
private static extern int getprivateprofilestring(
string lpappname,
string lpkeyname,
string lpdefault,
stringbuilder lpreturnedstring,
int nsize,
string lpfilename
);
[dllimport("kernel32")]
private static extern bool writeprivateprofilestring(
string lpappname,
string lpkeyname,
string lpstring,
string lpfilename
);
/// <summary>
///构造读写全路径为filename的ini文件的类
/// </summary>
/// <param name="filename"></param>
public rwini(string filename)
{
//
// 构造函数
//
filename=filename;
}
/// <summary>
/// 读取整型数据
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="def"></param>
/// <returns></returns>
public int getint(string section,string key,int def)
{
return getprivateprofileint(section,key,def,filename);
}
/// <summary>
/// 读取字符串数据
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="def"></param>
/// <returns></returns>
public string getstring(string section,string key,string def)
{
stringbuilder temp=new stringbuilder(1024);
getprivateprofilestring(section,key,def,temp,1024,filename);
return temp.tostring();
}
/// <summary>
/// 写入整型数据
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="ival"></param>
public void writeint(string section,string key,int ival)
{
writeprivateprofilestring(section,key,ival.tostring(),filename);
}
/// <summary>
/// 写入字符串数据
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="strval"></param>
public void writestring(string section,string key,string strval)
{
writeprivateprofilestring(section,key,strval,filename);
}
/// <summary>
/// 删除键值
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
public void delkey(string section,string key)
{
writeprivateprofilestring(section,key,null,filename);
}
/// <summary>
/// 删除模块
/// </summary>
/// <param name="section"></param>
public void delsection(string section)
{
writeprivateprofilestring(section,null,null,filename);
}
}
}