using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;
namespace Wjb.ReadOrWriteIniAndReg
{
///
/// RWIni 的摘要说明。
/// 读写ini文件类
/// 功能:读写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);
}
}
}
本文介绍了一个使用C#实现的INI文件读写类RWIni。该类提供了读取和写入INI文件中整型及字符串数据的功能,并支持删除特定键值或整个节。通过调用kernel32.dll中的API函数完成操作。
1759

被折叠的 条评论
为什么被折叠?



