常用的读写ini文件的类

80酷酷网    80kuku.com

  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);
}
}
}


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: