日期:2009-03-27  浏览次数:20408 次

另外后面大鸟也写了一个。但是不用API来完成的。

using System;
using System.Text;
using System.Runtime.InteropServices;


namespace EmuZone.Components
{
/// <summary>
/// INI操作类
/// </summary>
public class INI
{
/// <summary>
/// 申明API
/// </summary>
[DllImport("kernel32")]
static extern long WritePrivateProfileString(string lpAppName,string lpKeyName,string lpString,string lpFileName);
[DllImport("kernel32")]
static extern long GetPrivateProfileString(string lpAppName,string lpKeyName,string lpDefault,StringBuilder lpReturnedString,int nSize,string lpFileName);

private string strFileName;
private string strSection;
private string strKey;
private string strDefault;

/// <summary>
/// 构造函数
/// </summary>
public INI()
{
strFileName = "";
strSection = "";
strKey = "";
strDefault = "";
}

/// <summary>
/// 读取/设置INI文件区节
/// </summary>
public string Section
{
set{strSection = value;}
get{return strSection;}
}

/// <summary>
/// 读取/设置INI文件键值
/// </summary>
public string Key
{
set{strKey = value;}
get{return strKey;}
}

/// <summary>
/// 读取/写入INI文件键值的默认值
/// </summary>
public string Default
{
set{strDefault = value;}
get{return strDefault;}
}

/// <summary>
/// 读取/设置INI文件的位置
/// </summary>
public string FileName
{
set{strFileName = value;}
get{return strFileName;}
}

/// <summary>
/// 写INI文件
/// </summary>
/// <returns>非零整数</returns>
public long Write()
{
try
{
return WritePrivateProfileString(strSection,strKey,strDefault,strFileName);
}
catch
{
return 0;
}
}

/// <summary>
/// 以重载、写INI文件
/// </summary>
/// <param name="Section">INI文件区节</param>
/// <param name="Key">INI文件键值</param>
/// <param name="Default">INI文件值</param>
/// <param name="FileName">INI文件的位置</param>
/// <returns>非零整数</returns>
public long Write(string Section,string Key,string Default,string FileName)
{
try
{
return WritePrivateProfileString(Section,Key,Default,FileName);
}
catch
{
return 0;
}
}

/// <summary>
/// 读INI文件
/// </summary>
/// <returns>非负整数</returns>
public string Read()
{
try
{
const int intSize = 32768;
StringBuild