using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;
namespace Wjb.ReadOrWriteIniAndReg
{
///
/// RWIni 的摘要说明。
/// 读写ini文件类
/// 类库开发:吴剑冰
/// 时间:2003年10月20日
/// 功能:读写INI文件
///
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
);
///
///构造读写全路径为filename的ini文件的类
///
///
public RWIni(string filename)
{
//
// 构造函数
//
FileName=filename;
}
///
/// 读取整型数据
///
///
///
///
///
public int GetInt(string section,string key,int def)
{
return GetPrivateProfileInt(section,key,def,FileName);
}
///
/// 读取字符串数据
///
///
///
///
///
public string GetString(string section,string key,string def)
{
StringBuilder temp=new StringBuilder(1024);
GetPrivateProfileString(section,key,def,temp,1024,FileName);
return temp.ToString();
}
///
/// 写入整型数据
///
///
///
///
public void WriteInt(string section,string key,int iVal)
{
WritePrivateProfileString(section,key,iVal.ToString(),FileName);
}
///
/// 写入字符串数据
///
///
///
///
public void WriteString(string section,string key,string strVal)
{
WritePrivateProfileString(section,key,strVal,FileName);
}
///
/// 删除键值
///
///
///
public void DelKey(string section,string key)
{
WritePrivateProfileString(section,key,null,FileName);
}
///
/// 删除模块
///
///
public void DelSection(string section)
{
WritePrivateProfileString(section,null,null,FileName);
}
}
}

