Reputation:
I have this code which saves the location of Objects in an ini file but when I try to overwrite an already saved file, it doesn't delete the data currently in the file.
I have a popup which asks the user if they want to overwrite but it doesn't clear the whole file, how can I change it so it does?
procedure TForm1.SaveFile(Sender: TObject);
var
Dialog : TSaveDialog;
begin
Dialog := TSaveDialog.Create(self);
try
//dialog properties go here
Dialog.Filter := 'Title (*.ini)|*.ini';
Dialog.Options := Dialog.Options + [ofOverwritePrompt];
if Dialog.Execute then
begin
//any saving procedures go here if required
ShowMessage('File saved: ' + Dialog.FileName);
end
else
ShowMessage('Save file was cancelled');
finally
Dialog.Free;
end;
end;
The following procedure saves the objects to the file itself:
procedure TForm1.Save(const FileName: string);
var
Ini: TMemIniFile;
I: Integer;
procedure WhatYouAreSaving(Object: TButton);
var
Section: string;
begin
Section := 'Object'
//Properties you want to save go here
end;
begin
Ini := TMemIniFile.Create(FileName);
////For reference, the answer provided would go here
try
WhatYouAreSaving(Object);
Ini.UpdateFile;
finally
Ini.Free;
end;
end;
Upvotes: 0
Views: 1786
Reputation: 34929
To clear the contents of the ini-file, call Ini.Clear after Ini := TMemIniFile.Create(FileName);
Erases all data from the INI file in memory.
Call Clear to erase all data from the INI file that is currently buffered in memory. All sections, keys, and values are erased.
To erase a section in the ini-file, use TMemIniFile.EraseSection.
Upvotes: 3