Richtig gut wird die Sache, wenn die Ini-Datei dahin geschrieben wird, wohin sie unter
WIN32 gehört. Und die Zuweisungsorgie lässt sich abkürzen.
Delphi-Quellcode:
uses
IniFiles;
resourcestring
Env_AppData = 'APPDATA';
function GetEnv (varName: string): string;
var
buffer: array [byte] of char;
begin
StrPCopy (@buffer, varName);
SetString (Result, buffer, GetEnvironmentVariable (@buffer, buffer, SizeOf (buffer)));
end;
function GetAppData: string;
begin
Result := GetEnv(ENV_APPDATA);
end;
function TDemoForm.IniName: string;
begin
Result := IncludeTrailingPathDelimiter(GetAppData)
+ ChangeFileExt(ExtractFileName(Application.ExeName), '.ini');
end;
procedure TDemoForm.FormCreate(Sender: TObject);
var
ini: TIniFile;
i: integer;
begin
ini := TInifile.Create(IniName);
for i := 0 to ComponentCount - 1 do begin
if Components[i] is TEdit then
with Components[i] as TEdit do
Text := ini.ReadString(self.Name, Name, Text);
end;
ini.Free;
end;
procedure TDemoForm.FormDestroy(Sender: TObject);
var
ini: TIniFile;
i: integer;
begin
ini := TInifile.Create(IniName);
for i := 0 to ComponentCount - 1 do begin
if Components[i] is TEdit then
with Components[i] as TEdit do
ini.WriteString(self.Name, Name, Text);
end;
ini.Free;
end;
Grüße vom marabu