procedure TForm1.Button3Click(Sender: TObject);
begin
if RadioButton2.IsChecked then
Memo1.Lines.LoadFromFile(FIniFileName, TEncoding.Unicode)
else
Memo1.Lines.LoadFromFile(FIniFileName, TEncoding.ANSI);
MemoAdd('文件名是:' + sLineBreak + FIniFileName);
MemoAdd('');
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if FileExists(FIniFileName) then
begin
DeleteFile(FIniFileName);
MemoAdd('文件已成功删除。');
end
else
begin
MemoAdd('文件不存在或已删除。');
end;
end;
class function TIniRec4Rtti.GetIniAttribute(
Obj: TRttiObject): IniValueAttribute;
var
Attr: TCustomAttribute;
begin
for Attr in Obj.GetAttributes do
begin
if Attr is IniValueAttribute then
begin
exit(IniValueAttribute(Attr));
end;
end;
result := nil;
end;
class function TIniRec4Rtti.GetValue(var aValue: TValue): String;
begin
if aValue.Kind in [tkWChar, tkLString, tkWString, tkString, tkChar,
tkUString, tkInteger, tkInt64, tkFloat, tkEnumeration,
tkSet] then
result := aValue.ToString
else raise EIniRec4Rtti.Create('Type not Supported');
end;
class procedure TIniRec4Rtti.Load(FileName: String;RecPtr, TypPtr: Pointer);
var
ctx : TRttiContext;
RT : TRttiType;
RF : TRttiField;
Value : TValue;
IniValue : IniValueAttribute;
Ini : TIniFile;
Data : String;
begin
ctx := TRttiContext.Create;
try
Ini := TIniFile.Create(FileName);
try
RT := ctx.GetType( TypPtr );
if not Assigned(RT) or
(Assigned(RT) and not RT.IsRecord ) then
Exit;
for RF in RT.AsRecord.GetFields do
begin
IniValue := GetIniAttribute(RF);
if Assigned(IniValue) then
begin
Data := Ini.ReadString(IniValue.Section,IniValue.Name,IniValue.DefaultValue);
Value := RF.GetValue(RecPtr);
SetValue(Data,Value);
RF.SetValue(RecPtr,Value);
end;
end;
finally
Ini.Free;
end;
finally
ctx.Free;
end;
end;
class procedure TIniRec4Rtti.Save(FileName : String;RecPtr,TypPtr : Pointer);
var
ctx : TRttiContext;
RT : TRttiType;
RF : TRttiField;
Value : TValue;
IniValue : IniValueAttribute;
Ini : TIniFile;
Data : String;
begin
ctx := TRttiContext.Create;
try
Ini := TIniFile.Create(FileName);
try
RT := ctx.GetType(TypPtr);
if not Assigned(RT) or
(Assigned(RT) and not RT.IsRecord ) then
Exit;
for RF in RT.AsRecord.GetFields do
begin
IniValue := GetIniAttribute(RF);
if Assigned(IniValue) then
begin
Value := RF.GetValue(RecPtr);
Data := GetValue(Value);
Ini.WriteString(IniValue.Section,IniValue.Name,Data);
end;
end;
finally
Ini.Free;
end;
finally
ctx.Free;
end;
end;
class procedure TIniRec4Rtti.SetValue(aData: String; var aValue: TValue);
var
I : Integer;
begin
case aValue.Kind of
tkWChar,
tkLString,
tkWString,
tkString,
tkChar,
tkUString : aValue := aData;
tkInteger,
tkInt64 : aValue := StrToInt64Def(aData,0);
tkFloat : aValue := StrToFloatDef(aData,0.00);
tkEnumeration: aValue := TValue.FromOrdinal(aValue.TypeInfo,GetEnumValue(aValue.TypeInfo,aData));
tkSet: begin
i := StringToSet(aValue.TypeInfo,aData);
TValue.Make(@i, aValue.TypeInfo, aValue);
end;
else raise EIniRec4Rtti.Create('Type not Supported');
end;
end;
end.[/mw_shl_code]
参数文件
[mw_shl_code=delphi,true]unit uConfigClass;
interface
uses
IniRec4Rtti, SysUtils,Classes,System.Variants,System.IOUtils, System.IniFiles;
type
TSetupInfo = record
[IniValue('User','Username','')]
Username:string;
[IniValue('User','Password','')]
Password:string;
procedure Load;
procedure Save;
end;
implementation
{ TRemoteInfo }
procedure TSetupInfo.Load;
var
IniFile:TIniFile;
FilePath:string;
begin
FilePath:=IncludeTrailingPathDelimiter(TPath.GetDocumentsPath)+'Config.ini';
if not FileExists(FilePath) then
begin
try
IniFile := TIniFile.Create(FilePath);
finally
IniFile.Free;
end;
end;