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.