【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.

一些Delphi7的例子,包含下面内容:(高手就别看了,新手可看看,也是好多年没做了,又要做个小东西,练手做的例子) 有的是本人练习做的,少数是下载别人的或修改过的,也有1个似乎是明日科技的配套代码-此处没有删除,懒得删除直接打包了。 用这些分,只是用来备用下载其他东西而用,你觉得不值得,你留言分退给你(我来也不是常来这里),我认为是值得的。 还有一些其他的清单没罗列出来 如数据库表结构找不到,根据名字找这个文件名:EquipmentSQL_20170528_ok, 如果没就是没有了,因为一个连接服务器,一个是本机上的练习数据库,如没有请见谅 大致类型: cxGrid的几个例子、ini连接数据库、明日科技配套代码(文件上传下载)、 时间差相减(可参考Delphi函数里面的综合运用)、基本控件应用 cxGrid查找功能 cxGrid的列属性 cxGrid应用交流实例一_ok eg_Add_cbbValue_IntoMemo_test_20170401 eg_Check_Checkbox_Change eg_counter eg_cxGrid_Right_2 eg_cxGrid_Right_更细权限的bit值到表中 eg_cxGrid_SumRecord_1 eg_DCom_connection_20170606 eg_ImportToSQL eg_LoadRecord_Improve_Original eg_LoadRZCheckTree_1 eg_LoadRZCheckTree_连带设置_2 eg_LoadRZCheckTree_双击用户设置_del eg_LoadTree_1 eg_ReadIni_独立数据模块 eg_test_ImportExcelToSQL_ComOBJ_2 eg_update_bit_20170528 eg_报废时间月日格式只有1位时问题_20170609 eg_测试某列的条件改变另外列的Edit属性(cxGrid的例子) eg_对BoolToStr后的值进行再次转换_最后循环取bit值 eg_时间差计算_2 FileMS(明日科技的配套代码,关于文件上传下载的那个) EquipmentSQL_20170528_ok(如没数据库的,在这里查表结构,有的表结构不在这里,只能参考一下了)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

NolanDing

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

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

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

打赏作者

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

抵扣说明:

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

余额充值