RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.DeleteFile Function

Deletes a file from disk.

Pascal
function DeleteFile(const FileName: string): Boolean;
C++
Boolean DeleteFile(const AnsiString FileName);

SysUtils

DeleteFile deletes the file named by FileName from the disk. If the file cannot be deleted or does not exist, the function returns false.  

C++ Examples: 

 

/*
The following code prompts for confirmation before deleting
a file.  Create a file and then click on the button to select
the file with a TOpenDialog.  Click Yes to delete it.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  OpenDialog1->Title = "Delete File";
  if (OpenDialog1->Execute())
  {
    if (FileExists(OpenDialog1->FileName))
    {
      if (MessageDlg(
        ("Do you really want to delete " + ExtractFileName(OpenDialog1->FileName) + "?"),
        mtConfirmation,
        TMsgDlgButtons() << mbYes << mbNo,
        0,
        mbNo) == mrYes)
        DeleteFile(OpenDialog1->FileName);
    }
    else
      MessageDlg(("File " + ExtractFileName(OpenDialog1->FileName) + " does not exist."), mtConfirmation, TMsgDlgButtons() << mbOK, 0);
  }
}
/*
Click the button to open a TOpenDialog, then select a file 
to delete.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  OpenDialog1->Title = "Delete File";
  if (OpenDialog1->Execute())
  {
    if (FileExists(OpenDialog1->FileName))
      DeleteFile(OpenDialog1->FileName);
  }
}

 

Delphi Examples: 

{
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.
}
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;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!