FileExists (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following code prompts for confirmation before deleting a file. Create a file and then click the button to select the file with a TOpenDialog. Click Yes to delete it.

Code

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);
  }
}

Uses