【Delphi 开箱即用 7】读写ini文件的简单封装单元

INI文件是一种简单的文本文件,常用于存储配置信息。它由节(Sections)、键(Keys)和值(Values)组成,每个节用方括号括起来,节内可以包含多个键值对,键和值之间用等号连接。

在 Delphi 中,可以通过 TIniFile 类来读写 INI 文件。然而,为了提供更方便快捷的操作,本文提供了一种对 INI 文件读写的封装方法。

unit IniHelper;

interface

uses
  SysUtils, IniFiles, Windows;

type
  /// <summary>
  /// Delphi7兼容的INI文件操作辅助类
  /// </summary>
  TIniHelper = class
  private
    FIniFile: TIniFile;
    FFileName: string;
    procedure SafeCreateDir(const Dir: string);
  public
    constructor Create(const FileName: string = '');
    destructor Destroy; override;

    // 基础读写
    function ReadString(const Section, Key, Default: string): string;
    procedure WriteString(const Section, Key, Value: string);
    function ReadInteger(const Section, Key: string; Default: Integer): Integer;
    procedure WriteInteger(const Section, Key: string; Value: Integer);
    function ReadBool(const Section, Key: string; Default: Boolean): Boolean;
    procedure WriteBool(const Section, Key: string; Value: Boolean);

    // Delphi7兼容扩展
    function ReadFloat(const Section, Key: string; Default: Double): Double;
    procedure WriteFloat(const Section, Key: string; Value: Double);
    function ReadDateTime(const Section, Key: string; Default: TDateTime): TDateTime;
    procedure WriteDateTime(const Section, Key: string; Value: TDateTime);

    // 文件操作
    procedure UpdateFile;
    procedure DeleteKey(const Section, Key: string);
    procedure DeleteSection(const Section: string);
    function SectionExists(const Section: string): Boolean;
    function ValueExists(const Section, Key: string): Boolean;

    property FileName: string read FFileName;
  end;

implementation

{ TIniHelper }

constructor TIniHelper.Create(const FileName: string);
var
  DefaultFileName: string;
begin
  inherited Create;
  
  if FileName = '' then
    DefaultFileName := ExtractFilePath(ParamStr(0)) + 'Save.ini'
  else
    DefaultFileName := FileName;

  SafeCreateDir(ExtractFileDir(DefaultFileName));
  FIniFile := TIniFile.Create(DefaultFileName);
  FFileName := DefaultFileName;
end;

destructor TIniHelper.Destroy;
begin
  if Assigned(FIniFile) then
  begin
    FIniFile.UpdateFile;
    FreeAndNil(FIniFile);
  end;
  inherited;
end;

procedure TIniHelper.SafeCreateDir(const Dir: string);
begin
  if (Dir <> '') and (not DirectoryExists(Dir)) then
    if not CreateDir(Dir) then
      RaiseLastOSError;
end;

// 基础读写方法
function TIniHelper.ReadString(const Section, Key, Default: string): string;
begin
  Result := FIniFile.ReadString(Section, Key, Default);
end;

procedure TIniHelper.WriteString(const Section, Key, Value: string);
begin
  FIniFile.WriteString(Section, Key, Value);
end;

function TIniHelper.ReadInteger(const Section, Key: string; Default: Integer): Integer;
begin
  Result := FIniFile.ReadInteger(Section, Key, Default);
end;

procedure TIniHelper.WriteInteger(const Section, Key: string; Value: Integer);
begin
  FIniFile.WriteInteger(Section, Key, Value);
end;

function TIniHelper.ReadBool(const Section, Key: string; Default: Boolean): Boolean;
begin
  Result := FIniFile.ReadBool(Section, Key, Default);
end;

procedure TIniHelper.WriteBool(const Section, Key: string; Value: Boolean);
begin
  FIniFile.WriteBool(Section, Key, Value);
end;

// 扩展方法
function TIniHelper.ReadFloat(const Section, Key: string; Default: Double): Double;
begin
  Result := FIniFile.ReadFloat(Section, Key, Default);
end;

procedure TIniHelper.WriteFloat(const Section, Key: string; Value: Double);
begin
  FIniFile.WriteFloat(Section, Key, Value);
end;

function TIniHelper.ReadDateTime(const Section, Key: string; Default: TDateTime): TDateTime;
begin
  Result := FIniFile.ReadDateTime(Section, Key, Default);
end;

procedure TIniHelper.WriteDateTime(const Section, Key: string; Value: TDateTime);
begin
  FIniFile.WriteDateTime(Section, Key, Value);
end;

// 文件操作
procedure TIniHelper.UpdateFile;
begin
  if Assigned(FIniFile) then
    FIniFile.UpdateFile;
end;

procedure TIniHelper.DeleteKey(const Section, Key: string);
begin
  FIniFile.DeleteKey(Section, Key);
end;

procedure TIniHelper.DeleteSection(const Section: string);
begin
  FIniFile.EraseSection(Section);
end;

function TIniHelper.SectionExists(const Section: string): Boolean;
begin
  Result := FIniFile.SectionExists(Section);
end;

function TIniHelper.ValueExists(const Section, Key: string): Boolean;
begin
  Result := FIniFile.ValueExists(Section, Key);
end;

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NolanDing

请我喝杯咖啡吧,鼓励一下创作!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值