FileExists (Delphi)
Description
The following code prompts for confirmation before deleting a file. Create a file and then enter the path and file name in the textfields. Click the button to delete it.
VCL Code Snippet
procedure TForm1.Button1Click(Sender: TObject);
var FileName: string;
begin
Filename:= Edit1.Text;
if SysUtils.FileExists(FileName) then
begin
if MessageDlg(('Do you really want to delete ' + ExtractFileName(FileName) + '?'), mtConfirmation, [mbYes, mbNo], 0, mbNo) = IDYes then
DeleteFile(FileName);
end
else
MessageDlg(('File ' + ExtractFileName(FileName) + ' does not exist.'), mtConfirmation, [mbOK], 0);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Edit2.Text:= ExtractFilePath(Application.ExeName);
end;
FMX Code Snippet
var
FullFileName: string;
FileName: string;
begin
FullFileName := Edit1.Text;
FileName := ExtractFileName(FullFileName);
if FileExists(FullFileName) then
begin
TDialogService.MessageDialog(Format('Do you really want to delete "%s"?', [FileName]), TMsgDlgType.mtConfirmation, mbYesNo,
TMsgDlgBtn.mbNo, 0,
procedure (const AResult: TModalResult) begin
if AResult = mrYes then
DeleteFile(FullFileName);
end);
end
else
TDialogService.ShowMessage(Format('File "%s" does not exist.', [FileName]));
end;
Uses
- System.SysUtils.FileExists ( fr | de | ja )
- System.SysUtils.DeleteFile ( fr | de | ja )
- System.SysUtils.ExtractFilePath ( fr | de | ja )
- System.AnsiStrings.ExtractFilePath ( fr | de | ja )